Android开发——动态功能(四)fragment详解(一)

本文深入解析Fragment组件在Android应用开发中的作用及其实现方式。详细介绍了Fragment的生命周期、与Activity的关系,以及如何在布局文件中使用Fragment。通过示例代码展示了Fragment的创建、布局设置和与Activity的交互过程。

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

Fragment详解(一)

初识Fragment

  • Fragment有自己的生命周期
  • Fragment依赖于Activity
  • Fragment通过getActivity获取所在的Activity;Activity通过FragmentManager的findFragmentById()或findFragmentByTag()获取Fragment
  • Fragment和Activity是多对多的关系

Fragment显示在Activity的设计思路

  • 创建显示的Activity类
  • 创建Fragment类
  • 在Activity的布局文件布局FrameLayout(用以存放fragment)
  • 在Fragment的布局文件布局Fragment要显示的内容
  • 连接Fragment到Activity

示例代码

Fragment的java文件

package com.example.administrator.exercise.fragment;

import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.example.administrator.exercise.R;

import org.w3c.dom.Text;

public class FragmentA extends android.app.Fragment {

    private TextView tv;
    //返回一个视图文件,相当于setContextView一样
    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_a,container,false);  //参数:fragment的布局文件,container,false
        return view;
    }

    @Override
    public void onViewCreated(View view, Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        tv = view.findViewById(R.id.fm_tv);
    }

}

Fragment的布局文件

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/activity_fragment_a"
    android:layout_width="match_parent"
    android:gravity="center"
    android:layout_height="match_parent"
    tools:context="com.example.administrator.exercise.fragment.FragmentA">
    <Button
        android:layout_width="match_parent"
        android:layout_height="50dp"
        android:text="跳转Fragment"
        android:textSize="16sp"
        android:id="@+id/container_bt"/>
    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="我是Afragment"
        android:textSize="16sp"
        android:gravity="center"
        android:id="@+id/fm_tv"/>
</RelativeLayout>

activity的布局文件

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/activity_container"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.example.administrator.exercise.fragment.ContainerActivity">
    
    <FrameLayout
        android:id="@+id/container_fl"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        >

    </FrameLayout>
</RelativeLayout>

activity的java文件

package com.example.administrator.exercise.fragment;

        import android.support.v7.app.AppCompatActivity;
        import android.os.Bundle;
        import android.widget.Button;

        import com.example.administrator.exercise.R;

public class ContainerActivity extends AppCompatActivity {

    Button bt_jump;
    
    private FragmentA Afragment;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_container);
        
        bt_jump = (Button) findViewById(R.id.container_bt);

        //实例化Afragment
        Afragment = new FragmentA();
        //把Afragment添加到Activity中,记得调用commit
        getFragmentManager().beginTransaction().add(R.id.container_fl,Afragment).commitAllowingStateLoss();
    }
}

运行结果图

在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

狮子座的程序员

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值