Fragment的静态创建、动态创建

本文介绍了在Android中创建Fragment的基本方法,包括静态创建和动态创建。静态创建涉及为Fragment设计布局文件,而动态创建则涉及到在Activity运行时添加Fragment。

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

学习fragment就要知道怎么去创建一个fragment,创建fragment分为静态创建fragment和动态创建fragment

静态创建Fragment

1.首先创建一个fragment要创建的布局文件

<?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:id="@+id/text"
        android:gravity="center"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:text="這是一個靜態的fragment" />

</LinearLayout>

2.创建一个类,继承Fragment类,重写onCreateView方法,使用布局填充器获取fragment要显示的布局,这里导包要注意,如果你要导入的包是import android.app.Fragment;那么你的应用只能在android3.0以上的手机上使用。如果你导的包是import android.support.v4.app.Fragment就可以兼容在android3.0以下的手机上,同事关联这个fragment的activity必须继承FragmentActivity这个类,如果不继承这个类就会报AndroidRuntimeException

* E/AndroidRuntime( 5497): Caused by: android.app.Fragment$InstantiationException: Trying to instantiate
 a class com.example.work.StaticFragment that is not a Fragment
就是我们创建的那个fragment不是一个fragment,系统不认识这个fragment。

fragment的代码

public class StaticFragment extends Fragment{
	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
		return inflater.inflate(R.layout.fragment_layout,container,false);
	}

}
3.在关联这个fragment的Activity的布局文件中创建一个fragment控件
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="com.example.work.MainActivity" >

    <fragment android:layout_width="match_parent" 
        android:id="@+id/static_fragment"
        android:name="com.example.work.StaticFragment"
        android:layout_height="match_parent"/>

</RelativeLayout>

注意:这个fragment必须有一个name或class属性,指向我们自己的fragment类的全路径(包名+类名)

4.Activity的代码(我们这里什么也没有做)

public class MainActivity extends FragmentActivity {

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
	}
}
动态的创建fragment
1.创建一个fragment的布局文件

<?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:id="@+id/text"
        android:gravity="center"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:text="这是一个动态创建的fragment" />

</LinearLayout>
2.创建一个类,继承Fragment,导包的注意点与静态的是一样的
public class StaticFragment extends Fragment{
	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
		return inflater.inflate(R.layout.fragment_layout,container,false);
	}

}
3.先Activity的布局中创建一个存放fragment的容器(空间),我这里使用的是FrameLayout(什么布局均可)
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="com.example.work.MainActivity" >

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

</RelativeLayout>
注意:这里必须有一个id,我们在activity的java代码中才能取到
4.在java代码中动态的添加fragment,我们先通过getFragmentManager获取一个Fragment管理器,通过一个beginTransaction开启一个事务,通过事务添加fragment,执行commit这个事务才会提交。add方法第一个参数是存放fragment的容器,第二个参数是fragment的对象,第三个是tag(标志)这个参数可以有,可以没有,看我们的需要。如果我们以后还需要这个fragment对象,我们可以使用getFragmentManager().findFragmentByTag("dynamicFragment")获取fragment对象

public class MainActivity extends Activity {

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		FragmentManager manager = getFragmentManager();
		FragmentTransaction transaction = manager.beginTransaction();
		DynamicFragment fragment=new DynamicFragment();
		transaction.add(R.id.dynamic_fragment, fragment,"dynamicFragment");
		transaction.commit();
	}
}



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值