简体   繁体   English

用片段布局替换活动布局

[英]Replace Activity layout with Fragment Layout

I have an activity, in that mail layout I have button on click of that button I want to replace the entire activity layout with my fragment layout 我有一个活动,在该邮件布局中,单击该按钮时有一个按钮,我想用片段布局替换整个活动布局

how can I do this.? 我怎样才能做到这一点。?

Simplest way to replace your Activity layout is to create a Container Parent layout and set as the content to the Activity and then add the Fragment to the container Layout 替换Activity布局的最简单方法是创建一个Container Parent layout并将其设置为Activity的内容,然后将Fragment添加到container Layout

 // create a frame layout
 FrameLayout fragmentLayout = new FrameLayout(this);
 // set the layout params to fill the activity 
 fragmentLayout.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT));
 // set an id to the layout
 fragmentLayout.setId(1000); // some positive integer
 // set the layout as Activity content
 setContentView(fragmentLayout);
 // Finally , add the fragment
  getSupportFragmentManager()
        .beginTransaction()
        .add(1000,new TestFragment()).commit();  // 1000 - is the id set for the container layout

Using the reference of Libin's answer : His answer gives the following error: Expected Resource of type ID . 使用Libin的答案的参考:他的答案给出以下错误: ID类型的期望资源 Hence follow the following procedure 因此,请遵循以下步骤

Create a resource file in values called ids 在名为id的值中创建资源文件

<resources>
    <item name="fragmentLayout" type="id"/>
</resources>

Use this id to reference the id of the layout. 使用此ID来引用布局的ID。

// create a frame layout
FrameLayout fragmentLayout = new FrameLayout(this);
// set the layout params to fill the activity 
fragmentLayout.setLayoutParams(new  ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT));
// set an id to the layout
fragmentLayout.setId(R.id.fragmentLayout); // some positive integer
// set the layout as Activity content
setContentView(fragmentLayout);
// Finally , add the fragment
getSupportFragmentManager().beginTransaction().add(R.id.fragmentLayout,new TestFragment()).commit(); 

Another method to get id for a generated layout is fragmentLayout.setId(View.getGeneratedId()); 获取已生成布局的ID的另一种方法是fragmentLayout.setId(View.getGeneratedId()); . But it is problematic to access the id of the fragment outside the activity using this method. 但是使用这种方法在活动之外访问片段的ID是有问题的。 Hence, I would recommend the above method. 因此,我建议使用上述方法。

Assuming your Activity layout looks something like: 假设您的“ Activity布局如下所示:

<FrameLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/container"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />
</FrameLayout>

You can replace the contents of the FrameLayout with your Fragment : 您可以用Fragment替换FrameLayout的内容:

getFragmentManager()
        .beginTransaction()
        .replace(R.id.container, fragment)
        .commit();

If you want to be able to press 'back' to remove the fragment, add addToBackStack(null) to the FragmentTransaction . 如果您希望能够按“后退”来删除片段,请将addToBackStack(null)添加到FragmentTransaction See the docs for more details. 有关更多详细信息,请参阅文档

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM