2015-09-29 5 views
0

Я загружаю фрагмент в свой FrameLayout, и у меня есть кнопка в фрагменте. Когда нажимается кнопка, я хочу начать новое действие. Но когда я запускаю действие, кнопка все еще присутствует в новом действии. Как я могу это предотвратить?Вызов LinearLayout из 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"/> 

Фрагмент 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

Вы должны добавить свой код ... –

+0

Пожалуйста, добавьте свой код или предоставить некоторые ссылки. – dex

ответ

0

Это происходит потому, что ваш MainActivity2 макет является прозрачным и в стеке ниже вашего MainActivity, ваш фрагмент лежит поэтому кнопки этого фрагмента видно в вашем MainActivity2.

Сделайте одну вещь: поставить некоторые темный цвет фона не будет виден android:background="#ff0000" в LinearLayout из MainActivity2.xml

+0

Я нашел свою ошибку. Но спасибо за ответ –

Смежные вопросы