2015-09-29 1 views
0

Je charge un Fragment dans mon FrameLayout et j'ai un bouton dans le fragment. Lorsque le bouton est cliqué, je veux démarrer une nouvelle activité. Mais quand je démarre l'activité, le bouton est toujours présent dans la nouvelle activité. Comment puis-je empêcher cela?Appel d'un LinearLayout à partir d'un FrameLayout

MainActivity.Java

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
xmlns:app="http://schemas.android.com/apk/res-auto" 
xmlns:tools="http://schemas.android.com/tools" 
android:layout_width="match_parent" 
android:layout_height="match_parent" 
android:orientation="vertical" 
tools:context=".MainActivity" 
android:id="@+id/main"> 

<android.support.v7.widget.Toolbar 
    android:id="@+id/toolbar" 
    android:layout_width="match_parent" 
    android:layout_height="?attr/actionBarSize" 
    android:background="?attr/colorPrimary" 
    app:layout_scrollFlags="scroll|enterAlways" 
    app:popupTheme="@style/ThemeOverlay.AppCompat.Light" /> 

<android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:app="http://schemas.android.com/apk/res-auto" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:fitsSystemWindows="true" 
    android:id="@+id/drawerLayout"> 


    <FrameLayout 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:id="@+id/frame"/> 



    <android.support.design.widget.NavigationView 
     android:layout_width="wrap_content" 
     android:layout_height="match_parent" 
     android:layout_gravity="start" 
     android:fitsSystemWindows="true" 
     app:headerLayout="@layout/drawer_header" 
     app:menu="@menu/drawer" 
     android:id="@+id/navigation"/> 
</android.support.v4.widget.DrawerLayout> 

MainActivity2

<LinearLayout 
android:layout_width="match_parent" 
android:layout_height="match_parent" 
android:id="@+id/frame" 
android:orientation="vertical" 
xmlns:android="http://schemas.android.com/apk/res/android" > 
<TextView 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:text="Test"/> 

Fragment onCreate

public void onViewCreated(View view, Bundle savedInstanceState){ 
    super.onViewCreated(view, savedInstanceState); 

    final Button button = (Button) view.findViewById(R.id.button); 
    button.setOnClickListener(new View.OnClickListener() { 
     @Override 
     public void onClick(View view) { 
      Intent intent = new Intent(getActivity(),Main2Activity.class); 
      startActivity(intent); 

     } 
    }); 
} 

fragment.xml

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android" 
xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" 
android:layout_height="match_parent" tools:context="com.anonymous.kalanjali2015.HomeFragment"> 

<Button 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:text="next" 
    android:id="@+id/button" /> 

+0

Vous devez ajouter votre code ... –

+0

S'il vous plaît ajouter votre code ou fournir un lien. – dex

Répondre

0

Cela se produit parce que votre mise en page MainActivity2 est transparente et dans la pile en dessous de votre MainActivity, votre fragment se trouve donc des boutons de ce fragment est visible dans votre MainActivity2.

faire une chose: mettre un peu de couleur de fond sombre, il ne sera pas visible dans le android:background="#ff0000"LinearLayout de MainActivity2.xml

+0

J'ai trouvé mon erreur. Mais merci pour la réponse –