添加Fragment的两种方式

1、在xml文件中直接添加。 
在activity_main.xml中添加如下内容:

    <fragment
        android:id="@+id/activity_fragmentdemo_head"
        android:name="com.example.administrator.demoandroid.HeadFragment"
        android:layout_margin="@dimen/activity_vertical_margin"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"></fragment>
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6

HeadFragment的代码如下:

public class HeadFragment extends Fragment {

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        return inflater.inflate(R.layout.fragment_head,container,false);
    }
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8

只要如上配置,在MainActivity中即可嵌入R.layout.fragment_head界面。

2、代码动态添加。 
在activity_main.xml中添加:

    <FrameLayout
        android:id="@+id/fragment_container"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content">
    </FrameLayout>
  • 1
  • 2
  • 3
  • 4
  • 5
  • 1
  • 2
  • 3
  • 4
  • 5

以下为Activity中动态添加Fragment的代码:

   public void addFragment(View view){

        ContainerFragment containerFragment = new ContainerFragment();

        getFragmentManager()
                .beginTransaction()
                .add(R.id.fragment_container,containerFragment)
                .commit();
    }
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9

FragmentLayout标签在开始时是不会显示出来的,只有调用了动态添加Fragment后,被添加的内容才会显示在MainActivity中。

1、在xml文件中直接添加。 
在activity_main.xml中添加如下内容:

    <fragment
        android:id="@+id/activity_fragmentdemo_head"
        android:name="com.example.administrator.demoandroid.HeadFragment"
        android:layout_margin="@dimen/activity_vertical_margin"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"></fragment>
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6

HeadFragment的代码如下:

public class HeadFragment extends Fragment {

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        return inflater.inflate(R.layout.fragment_head,container,false);
    }
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8

只要如上配置,在MainActivity中即可嵌入R.layout.fragment_head界面。

2、代码动态添加。 
在activity_main.xml中添加:

    <FrameLayout
        android:id="@+id/fragment_container"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content">
    </FrameLayout>
  • 1
  • 2
  • 3
  • 4
  • 5
  • 1
  • 2
  • 3
  • 4
  • 5

以下为Activity中动态添加Fragment的代码:

   public void addFragment(View view){

        ContainerFragment containerFragment = new ContainerFragment();

        getFragmentManager()
                .beginTransaction()
                .add(R.id.fragment_container,containerFragment)
                .commit();
    }
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9

FragmentLayout标签在开始时是不会显示出来的,只有调用了动态添加Fragment后,被添加的内容才会显示在MainActivity中。

猜你喜欢

转载自blog.csdn.net/a600849155/article/details/77854821