Android中fragment的静态和动态使用

本文详细介绍了在Android应用中如何使用Fragment,包括静态方式创建标题和内容的fragment,以及动态添加Fragment的实践,涉及到MainActivity、TitleFragment和ContentFragment的使用,以及对应的XML布局文件。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

一、静态fragment

创建标题和内容的fragment:

package com.example.administrator.fragment01.xw.fragment;

import android.app.Fragment;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.RelativeLayout;
import android.widget.Toast;

import com.example.administrator.fragment01.R;

/**
 * 创建和使用fragment的步骤:
 * 1.创建子类继承fragment
 * 2.重写onCreateView()方法 该方法主要定义fragment布局 以view对象的形式返回fragment视图
 * 3.将fragment引入到Activity中
 */

public class TitleFragment extends Fragment {
    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, Bundle savedInstanceState) {
        /**
         * 表示fragment第一次创建绘制用户界面时系统回调的方法
         *  返回值 view 表示当前加载fragment视图 如果fragment不提供视图可以返回null
         *  LayoutInflater inflater,表示布局填充器或加载器 将xml对象转换成view对象
         *      ViewGroup container,表示当前fragment插入activity的布局视图对象
         *      Bundle savedInstanceState 表示存储上一个fragment的信息
         */
        //表示将指定资源的xml文件转换成具体的view对象 inflate(表示加载xml文件的资源id,null )
        View view = inflater.inflate(R.layout.fragment_title,null);
        RelativeLayout layout = view.findViewById(R.id.rl_layout);
        layout.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Toast.makeText(getActivity(),"我是标题",Toast.LENGTH_SHORT).show();
            }
        });
        return view;
    }
}

package com.example.administrator.fragment01.xw.fragment;

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

import com.example.administrator.fragment01.R;

/**
 * Created by Administrator on 2018/4/27.
 */

public class ContentFragment extends Fragment {
    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, Bundle savedInstanceState) {
        return inflater.inflate(R.layout.fragment_content,null);
    }
}

XML布局文件:

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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="com.example.administrator.fragment01.MainActivity">

    <fragment
        android:id="@+id/fragment_title"
        android:layout_width="match_parent"
        android:layout_height="50dp"
        android:name="com.example.administrator.fragment01.xw.fragment.TitleFragment"
    />
    <fragment
        android:id="@+id/fragment_content"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_below="@id/fragment_title"
        android:name="com.example.administrator.fragment01.xw.fragment.ContentFragment"
        />
    <!-- android:name="表示引用的fragment的包名.类名" -->
</RelativeLayout>

fragment_title.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="50dp"
    android:id="@+id/rl_layout"
    android:background="@android:color/holo_blue_dark"
    >

    <ImageView
        android:id="@+id/iv_back"
        android:layout_width="35dp"
        android:layout_height="35dp"
        android:layout_centerVertical="true"
        android:paddingLeft="15dp"
        android:scaleType="centerCrop"
        android:src="@drawable/back"
        />
    <TextView
        android:id="@+id/tv_title"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true"
        android:text="我是标题"
        android:textSize="20sp"
        android:textColor="#ffffff"
        />
</RelativeLayout>

fragment_content.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    >
    <TextView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:gravity="center"
        android:text="我是fragment内容界面"
        android:textSize="20sp"
        android:textStyle="bold"
        />
</LinearLayout>

二、动态添加Fragment

MainActivity

package com.xw.fragment02;

import android.app.Activity;
import android.app.FragmentManager;
import android.app.FragmentTransaction;
import android.os.Bundle;
import android.view.Window;

import com.xw.fragment02.fragment.ContentFragment;
import com.xw.fragment02.fragment.TitleFragment;

public class MainActivity extends Activity {
    /**
     * 演示fragment的动态使用
     *
     * 案例效果:
     *  在activity界面中有两个fragment 标题 内容
     */
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.activity_main);
        //1.创建Fragment的管理器对象
        FragmentManager manager = getFragmentManager();
        //2.获取Fragment的事务对象并且开启事务
        FragmentTransaction transaction = manager.beginTransaction();
        //3.调用事务中相应的动态操作Fragment的方法执行 add(表示fragment动态添加位置的资源id,表示添加的fragment对象)
        transaction.add(R.id.title_layout,new TitleFragment());//将TitleFragment动态添加到title_layout的位置
        transaction.add(R.id.content_layout,new ContentFragment());
        //transaction.remove(arg0);     remove(需要移除的fragment对象)
        //transaction.replace(arg0,arg1)    replace(表示替换fragment位置的资源id,表示替换fragment对象)
        //4.提交事务
        transaction.commit();
    }
}

TitleFragment

package com.xw.fragment02.fragment;

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

import com.xw.fragment02.R;

/**
 * 标题fragment
 *
 */

public class TitleFragment extends Fragment {
    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_title,null);
        return view;
    }
}

ContentFragment

package com.xw.fragment02.fragment;

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

import com.xw.fragment02.R;

/**
 * Created by Administrator on 2018/4/27.
 */

public class ContentFragment extends Fragment {
    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_content,null);
        return view;
    }
}

布局文件:

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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="com.xw.fragment02.MainActivity">

    <LinearLayout
        android:id="@+id/title_layout"
        android:layout_width="match_parent"
        android:layout_height="50dp"
        android:orientation="vertical"></LinearLayout>

    <LinearLayout
        android:id="@+id/content_layout"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_below="@id/title_layout"
        android:orientation="vertical"></LinearLayout>

</RelativeLayout>

fragment_title.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="50dp"
    android:background="@android:color/holo_red_light"
    >
    <ImageView
        android:id="@+id/iv_back"
        android:layout_width="35dp"
        android:layout_height="35dp"
        android:layout_centerVertical="true"
        android:paddingLeft="15dp"
        android:scaleType="centerCrop"
        android:src="@drawable/back"
        />
    <TextView
        android:id="@+id/tv_title"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true"
        android:text="我是动态引入标题"
        android:textSize="20sp"
        android:textColor="#ffffff"
        />
</RelativeLayout>

fragment_content.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <TextView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:gravity="center"
        android:text="我是动态引入内容"
        android:textSize="20sp"
        android:textStyle="bold" />
</LinearLayout>

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值