Android 向活动添加片段后,活动布局仍然可见

Android 向活动添加片段后,活动布局仍然可见,android,android-fragments,Android,Android Fragments,这是一个很蹩脚的问题,但我真的不明白为什么会这样,所以请帮我解决 我有一个简单的活动,我动态地向它添加了一个片段。问题是,一旦片段添加到活动,则活动布局也可见。为什么会发生这种情况 MainActivity.java public class MainActivity extends AppCompatActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(save

这是一个很蹩脚的问题,但我真的不明白为什么会这样,所以请帮我解决

我有一个简单的活动,我动态地向它添加了一个片段。问题是,一旦片段添加到活动,则活动布局也可见。为什么会发生这种情况

MainActivity.java

public class MainActivity extends AppCompatActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    Button btnLoad = (Button) findViewById(R.id.btn1);

    View.OnClickListener listener = new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            FragmentManager fragmentManager = getFragmentManager();
            FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
            HelloFragment hello = new HelloFragment();
            fragmentTransaction.add(R.id.rootView, hello, "HELLO");
            fragmentTransaction.commit();
        }
    };

    btnLoad.setOnClickListener(listener);
} 
}
public class HelloFragment extends Fragment {
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, 
Bundle savedInstanceState) {                       
    /** Inflating the layout for this fragment **/
    View v = inflater.inflate(R.layout.hello_fragment_layout, null);
    return v;
}
}
活动\u main.xml

<LinearLayout 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=".MainActivity">
<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:id="@+id/rootView"
    android:orientation="vertical">
    <TextView
        android:text="@string/hello_world"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />
    <Button
        android:id="@+id/btn1"
        android:layout_height="wrap_content"
        android:layout_width="wrap_content"
        android:text="OK"/>
</LinearLayout>
</LinearLayout>
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
  xmlns:android="http://schemas.android.com/apk/res/android"      
  android:orientation="vertical" android:layout_width="match_parent"
  android:layout_height="match_parent">
  <TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:gravity="center"
    android:text="fragment 1"/>
 </LinearLayout>
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
  xmlns:android="http://schemas.android.com/apk/res/android"      
  android:orientation="vertical" android:layout_width="match_parent"
  android:layout_height="match_parent">
  <TextView
        android:text="@string/hello_world"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />
    <Button
        android:id="@+id/btn1"
        android:layout_height="wrap_content"
        android:layout_width="wrap_content"
        android:text="OK"/>
 </LinearLayout>
hello\u fragment\u layout.xml

<LinearLayout 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=".MainActivity">
<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:id="@+id/rootView"
    android:orientation="vertical">
    <TextView
        android:text="@string/hello_world"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />
    <Button
        android:id="@+id/btn1"
        android:layout_height="wrap_content"
        android:layout_width="wrap_content"
        android:text="OK"/>
</LinearLayout>
</LinearLayout>
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
  xmlns:android="http://schemas.android.com/apk/res/android"      
  android:orientation="vertical" android:layout_width="match_parent"
  android:layout_height="match_parent">
  <TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:gravity="center"
    android:text="fragment 1"/>
 </LinearLayout>
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
  xmlns:android="http://schemas.android.com/apk/res/android"      
  android:orientation="vertical" android:layout_width="match_parent"
  android:layout_height="match_parent">
  <TextView
        android:text="@string/hello_world"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />
    <Button
        android:id="@+id/btn1"
        android:layout_height="wrap_content"
        android:layout_width="wrap_content"
        android:text="OK"/>
 </LinearLayout>


为片段线性布局设置背景色并使其可单击

为片段线性布局设置背景色并使其可单击

片段只是布局上的另一个元素。将其添加到布局不会自动删除其他视图。如果希望片段替换
rootView
的内容,则需要先删除
rootView
的子视图。比如:

((LinearLayout)findViewById(R.id.rootView)).removeAllViews();
因此,您的
onClick
方法应该如下所示:

    @Override
    public void onClick(View v) {
        ((LinearLayout)findViewById(R.id.rootView)).removeAllViews();

        FragmentManager fragmentManager = getFragmentManager();
        FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
        HelloFragment hello = new HelloFragment();
        fragmentTransaction.add(R.id.rootView, hello, "HELLO");
        fragmentTransaction.commit();
    }

片段只是布局上的另一个元素。将其添加到布局不会自动删除其他视图。如果希望片段替换
rootView
的内容,则需要先删除
rootView
的子视图。比如:

((LinearLayout)findViewById(R.id.rootView)).removeAllViews();
因此,您的
onClick
方法应该如下所示:

    @Override
    public void onClick(View v) {
        ((LinearLayout)findViewById(R.id.rootView)).removeAllViews();

        FragmentManager fragmentManager = getFragmentManager();
        FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
        HelloFragment hello = new HelloFragment();
        fragmentTransaction.add(R.id.rootView, hello, "HELLO");
        fragmentTransaction.commit();
    }
activity_main.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" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/rootView"
android:orientation="vertical">
<TextView
    android:text="@string/hello_world"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content" />
<Button
    android:id="@+id/btn1"
    android:layout_height="wrap_content"
    android:layout_width="wrap_content"
    android:text="OK"/>
</LinearLayout>

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

</FrameLayout>
</FrameLayout>
activity_main.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" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/rootView"
android:orientation="vertical">
<TextView
    android:text="@string/hello_world"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content" />
<Button
    android:id="@+id/btn1"
    android:layout_height="wrap_content"
    android:layout_width="wrap_content"
    android:text="OK"/>
</LinearLayout>

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

</FrameLayout>
</FrameLayout>

约定是使用fragmentTransaction.replace()/add()的FrameLayout


在MainActivity布局文件中不应该有TextView和Button之类的视图。相反,将它们放在另一个片段中,并在mainActivityXML中有一个框架布局。首先用TextView/Button加载片段,然后用新片段单击按钮调用replace。

惯例是使用fragmentTransaction.replace()/add()创建框架布局


在MainActivity布局文件中不应该有TextView和Button之类的视图。相反,将它们放在另一个片段中,并在mainActivityXML中有一个框架布局。首先用TextView/按钮加载片段,然后调用替换按钮,点击新片段。

我有一个解决方案,您可以做的是创建两个片段,一个用于文本视图,另一个用于按钮,在活动打开时默认显示,另一个已创建为HelloFragment

从ActivityonCreate()方法调用默认的textview按钮的第一个片段,并从该按钮的单击侦听器调用HelloFramgent

步骤1:用任意名称包装另一个片段MainFragment,其xml布局如下:-

main\u fragment\u layout.xml

<LinearLayout 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=".MainActivity">
<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:id="@+id/rootView"
    android:orientation="vertical">
    <TextView
        android:text="@string/hello_world"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />
    <Button
        android:id="@+id/btn1"
        android:layout_height="wrap_content"
        android:layout_width="wrap_content"
        android:text="OK"/>
</LinearLayout>
</LinearLayout>
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
  xmlns:android="http://schemas.android.com/apk/res/android"      
  android:orientation="vertical" android:layout_width="match_parent"
  android:layout_height="match_parent">
  <TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:gravity="center"
    android:text="fragment 1"/>
 </LinearLayout>
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
  xmlns:android="http://schemas.android.com/apk/res/android"      
  android:orientation="vertical" android:layout_width="match_parent"
  android:layout_height="match_parent">
  <TextView
        android:text="@string/hello_world"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />
    <Button
        android:id="@+id/btn1"
        android:layout_height="wrap_content"
        android:layout_width="wrap_content"
        android:text="OK"/>
 </LinearLayout>
步骤2:从activityonCreate()调用main片段

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    FragmentManager fragmentManager = getFragmentManager();
    FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
    MainFragment mainFragment = new MainFragment();
    fragmentTransaction.add(R.id.rootView, mainFragment, "MAIN_FRAGMENT");
    fragmentTransaction.commit();
}

我有一个解决方案,您可以做的是创建两个片段,一个用于文本视图按钮,在活动打开时显示为默认值,另一个是您已经创建为HelloFragment的片段

从ActivityonCreate()方法调用默认的textview按钮的第一个片段,并从该按钮的单击侦听器调用HelloFramgent

步骤1:用任意名称包装另一个片段MainFragment,其xml布局如下:-

main\u fragment\u layout.xml

<LinearLayout 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=".MainActivity">
<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:id="@+id/rootView"
    android:orientation="vertical">
    <TextView
        android:text="@string/hello_world"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />
    <Button
        android:id="@+id/btn1"
        android:layout_height="wrap_content"
        android:layout_width="wrap_content"
        android:text="OK"/>
</LinearLayout>
</LinearLayout>
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
  xmlns:android="http://schemas.android.com/apk/res/android"      
  android:orientation="vertical" android:layout_width="match_parent"
  android:layout_height="match_parent">
  <TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:gravity="center"
    android:text="fragment 1"/>
 </LinearLayout>
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
  xmlns:android="http://schemas.android.com/apk/res/android"      
  android:orientation="vertical" android:layout_width="match_parent"
  android:layout_height="match_parent">
  <TextView
        android:text="@string/hello_world"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />
    <Button
        android:id="@+id/btn1"
        android:layout_height="wrap_content"
        android:layout_width="wrap_content"
        android:text="OK"/>
 </LinearLayout>
步骤2:从activityonCreate()调用main片段

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    FragmentManager fragmentManager = getFragmentManager();
    FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
    MainFragment mainFragment = new MainFragment();
    fragmentTransaction.add(R.id.rootView, mainFragment, "MAIN_FRAGMENT");
    fragmentTransaction.commit();
}

在片段中构建用户界面(在大多数情况下,一个片段等于一个屏幕),然后使用活动来排列和显示这些片段。
选中此项:

在片段中构建用户界面(在大多数情况下,一个片段等于一个屏幕),然后使用活动来排列和显示这些片段。
选中此项:

您的rootview充当片段和活动视图的容器。检查它并为片段创建单独的视图。

您的rootview充当片段和活动视图的容器。检查它并为片段创建单独的视图。

您有两个线性布局,一个在另一个内部,因此需要执行以下步骤:

  • 将id设置为1线性布局根视图,并在子视图旁边

  • 查找您的子视图:

    View view = findViewById(R.id.child_view);
    
    view.setVisibility(View.INVISIBLE);
    
  • 用片段替换您的rootView:

    getSupportFragmentManager().beginTransaction().add(R.id.rootview, new blankFragment())
        .addToBackStack(null).commit();
    
  • 然后覆盖:

    @override
    public void onBackPressed() {
        android.support.v4.app.FragmentManager fm = getSupportFragmentManager();
    
        if (fm.getBackStackEntryCount() > 0) {
            view.setVisibility(View.VISIBLE);
        }
    
        super.onBackPressed();
    }
    

  • 您有两个线性布局,一个在另一个内部,因此需要执行以下步骤:

  • 将id设置为1线性布局根视图,并在子视图旁边

  • 查找您的子视图:

    View view = findViewById(R.id.child_view);
    
    view.setVisibility(View.INVISIBLE);
    
  • 用片段替换您的rootView:

    getSupportFragmentManager().beginTransaction().add(R.id.rootview, new blankFragment())
        .addToBackStack(null).commit();
    
  • 然后覆盖:

    @override
    public void onBackPressed() {
        android.support.v4.app.FragmentManager fm = getSupportFragmentManager();
    
        if (fm.getBackStackEntryCount() > 0) {
            view.setVisibility(View.VISIBLE);
        }
    
        super.onBackPressed();
    }
    

  • 我知道现在回答这个问题已经很晚了,但对于那些仍在寻找答案的人,请在活动布局中执行以下操作:

    <RelativeLayout 
    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=".MainActivity">
    <LinearLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    
    android:orientation="vertical">
    <TextView
        android:text="@string/hello_world"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />
    <Button
        android:id="@+id/btn1"
        android:layout_height="wrap_content"
        android:layout_width="wrap_content"
        android:text="OK"/>
     </LinearLayout>
    
    <LinearLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:id="@+id/rootView"
    android:orientation="vertical">
     </RelativeLayout>
    
    <RelativeLayout
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@+id/activity_main"
        android:layout_width="match_parent"
        android:layout_height="match_parent">
    
    <TextView
        android:id="@+id/title"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:gravity="center"
        android:text="Activity Title"
        android:layout_marginTop="10dp"
        android:layout_marginBottom="20dp"
        android:textSize="24sp" />
    
    <Button
        android:id="@+id/button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/title"
        android:layout_centerHorizontal="true"
        android:textAllCaps="false"
        android:text="Button1" />
    
    <Button
        android:id="@+id/button2"
        android:textAllCaps="false"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/button1"
        android:layout_centerHorizontal="true"
        android:text="Button2" />
    
    <!-- Fragment holder, with elevation -->
    <FrameLayout
        android:translationZ="2dp"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:id="@+id/fragHolder"
        />
    
    
    
    和碎片布局如下所示:

    <?xml version="1.0" encoding="utf-8"?>
     <LinearLayout
      xmlns:android="http://schemas.android.com/apk/res/android"      
    android:orientation="vertical" android:layout_width="match_parent"
     android:background="@color/white"    <!--Add this-->
      android:layout_height="match_parent">
     <TextView
        android:text="@string/hello_world"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />
    <Button
        android:id="@+id/btn1"
        android:layout_height="wrap_content"
        android:layout_width="wrap_content"
        android:text="OK"/>
    </LinearLayout>
    
    
    
    我知道现在回答这个问题已经很晚了,但对于那些仍在寻找答案的人,请在活动布局中执行以下操作:

    <RelativeLayout 
    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=".MainActivity">
    <LinearLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    
    android:orientation="vertical">
    <TextView
        android:text="@string/hello_world"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />
    <Button
        android:id="@+id/btn1"
        android:layout_height="wrap_content"
        android:layout_width="wrap_content"
        android:text="OK"/>
     </LinearLayout>
    
    <LinearLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:id="@+id/rootView"
    android:orientation="vertical">
     </RelativeLayout>
    
    <RelativeLayout
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@+id/activity_main"
        android:layout_width="match_parent"
        android:layout_height="match_parent">
    
    <TextView
        android:id="@+id/title"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:gravity="center"
        android:text="Activity Title"
        android:layout_marginTop="10dp"
        android:layout_marginBottom="20dp"
        android:textSize="24sp" />
    
    <Button
        android:id="@+id/button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/title"
        android:layout_centerHorizontal="true"
        android:textAllCaps="false"
        android:text="Button1" />
    
    <Button
        android:id="@+id/button2"
        android:textAllCaps="false"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/button1"
        android:layout_centerHorizontal="true"
        android:text="Button2" />
    
    <!-- Fragment holder, with elevation -->
    <FrameLayout
        android:translationZ="2dp"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:id="@+id/fragHolder"
        />
    
    
    
    和碎片布局如下所示:

    <?xml version="1.0" encoding="utf-8"?>
     <LinearLayout
      xmlns:android="http://schemas.android.com/apk/res/android"      
    android:orientation="vertical" android:layout_width="match_parent"
     android:background="@color/white"    <!--Add this-->
      android:layout_height="match_parent">
     <TextView
        android:text="@string/hello_world"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />
    <Button
        android:id="@+id/btn1"
        android:layout_height="wrap_content"
        android:layout_width="wrap_content"
        android:text="OK"/>
    </LinearLayout>
    
    
    
    活动视图没有针对片段的视图进行遮罩,因此两者都会出现。一种解决方案是将片段加载到主活动布局中的一个框架中,该框架具有轻微的标高(2 dp)。加载碎片时,其底部高程将使其始终绘制在活动上方。使用颜色设置片段父容器以隐藏活动视图

    主活动布局:

    <RelativeLayout 
    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=".MainActivity">
    <LinearLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    
    android:orientation="vertical">
    <TextView
        android:text="@string/hello_world"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />
    <Button
        android:id="@+id/btn1"
        android:layout_height="wrap_content"
        android:layout_width="wrap_content"
        android:text="OK"/>
     </LinearLayout>
    
    <LinearLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:id="@+id/rootView"
    android:orientation="vertical">
     </RelativeLayout>
    
    <RelativeLayout
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@+id/activity_main"
        android:layout_width="match_parent"
        android:layout_height="match_parent">
    
    <TextView
        android:id="@+id/title"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:gravity="center"
        android:text="Activity Title"
        android:layout_marginTop="10dp"
        android:layout_marginBottom="20dp"
        android:textSize="24sp" />
    
    <Button
        android:id="@+id/button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/title"
        android:layout_centerHorizontal="true"
        android:textAllCaps="false"
        android:text="Button1" />
    
    <Button
        android:id="@+id/button2"
        android:textAllCaps="false"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/button1"
        android:layout_centerHorizontal="true"
        android:text="Button2" />
    
    <!-- Fragment holder, with elevation -->
    <FrameLayout
        android:translationZ="2dp"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:id="@+id/fragHolder"
        />
    
    
    
    片段布局具有背景色:

    <RelativeLayout 
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="#fff"
        android:layout_margin="10dp">
    
    <TextView
        android:id="@+id/title"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:gravity="center"
        android:text="Test Background Task"
        android:textSize="24sp" />
    
    </RelativeLayout>