activity中添加一个fragment

核心内容

  1. 使用FragmeLayout作为fragment的显示容器。
  2. 为什么要用FragmeLayout?
  3. 第一种方法 :布局代码添加fragment
  4. 第二种方法 :java代码添加fragment
    原因:实践中常选用 FrameLayout 作为容器。(暂时未清楚具体原因)

第一种方法 :布局代码添加fragment

activity_main.xml(里面有两个fragment)

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">
    <fragment
        android:id="@+id/fragment2"
        android:name="com.example.tnt.myapplication.ExampleFragment"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        tools:layout="@layout/fragment1" />
    <fragment
        android:id="@+id/fragment3"
        android:name="com.example.tnt.myapplication.ExampleFragment"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        tools:layout="@layout/fragment1" />
</LinearLayout>

创建控制fragment的类

import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

public class ExampleFragment extends Fragment {
    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        return inflater.inflate(R.layout.fragment1,container,false);  // 此处的布局文件是普通的线性布局(此博客忽略)
    }
}

运行结果

这里写图片描述


第二种方法 :java代码添加fragment
activity_main.xml     // 线性布局里添加一个FragmeLayout来显示要显示的fragment

<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"
    tools:context=".MainActivity">

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

</LinearLayout>

MainActivity.java      // 程序启动添加一个fragment

public class MainActivity extends AppCompatActivity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        getSupportFragmentManager()    //
                .beginTransaction()
                .add(R.id.fragment_container,new TitleFragment())   // 此处的R.id.fragment_container是要盛放fragment的父容器
                .commit();
    }
}

TitleFragment .java      //fragment的java文件

public class TitleFragment extends Fragment {  //  Fragment引入的是v4包
    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.layout_fragment,container,false); //  此处的布局文件是普通的线性布局(此博客忽略)
        return view;
    }
}

PS:
**如果要用另一个fragment替换当前的fragment,
需新建一个布局文件,
新建一个Fragment类,在这个要被替换的Fragment类的onCreateView()中写如下东西**


        view.findViewById(R.id.replaceCurrentFragment).setOnClickListener(new View.OnClickListener() {//此处的id是layout_fragment.xml 的按钮 ,点击替换要被替换的fragment
            @Override
            public void onClick(View v) {
                getFragmentManager()   //所得到的是所在fragment 的父容器的管理器
                        .beginTransaction()
                        .addToBackStack()   //添加到后退栈   
                        //此处的id,是FrameLayout的id
                        .replace(R.id.fragment_container,new anotherFragment())   //anotherFragment()为新建的fragment的java文件,OnCreateView里面把.xml文件添加
                        .commit();
            }
        });

猜你喜欢

转载自blog.csdn.net/qq_38340601/article/details/82082135