添加Fragment

本文详细介绍了如何在FragmentActivity中通过创建ContentFragment并利用FragmentManager动态添加Fragment到界面上的操作流程。

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

FragmentActivity.java

public class FragmentActivity extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        setContentView(R.layout.fragment_activity);

        Button button = (Button) findViewById(R.id.bt_fragment);
        button.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View arg0) {
                // TODO Auto-generated method stub
                //动态添加fragment
                //创建Fragment对象
                ContentFragment contentFragment = new ContentFragment();
                //创建管理fragment事务的对象
                FragmentManager manager = getFragmentManager();
                //开启事务
                FragmentTransaction beginTransaction = manager.beginTransaction();
                //事务添加fragment
                beginTransaction.add(R.id.linearLayout, contentFragment);
                //提交事务
                beginTransaction.commit();
            }
        });
    }
}

ContentFragment.java(ContentFragment.java差不多)

public class ContentFragment extends Fragment {

@Override
public void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
Log.e("action=", "onCreate");
}

/**
 * fragment放布局的方法
 */
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// TODO Auto-generated method stub
Log.e("action=", "onCreateView");

View view = inflater.inflate(R.layout.fragment_content, container, false);

return view;
}
}

content_activity.xml

<LinearLayout
    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:orientation="horizontal">

    <Button 
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="addFragment"
        android:id="@+id/bt_fragment"/>

    <!-- 静态方法定义fragment -->
<!--     <fragment  -->
<!--         android:layout_width="0dp" -->
<!--         android:layout_height="match_parent" -->
<!--         android:layout_weight="1" -->
<!--         android:id="@+id/contentFragment" -->
<!--         android:name="com.fragment.ContentFragment"/> -->

    <fragment 
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_weight="1"
        android:id="@+id/titleFragment"
        android:name="com.fragment.TitleFragment"/>

    <LinearLayout
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_weight="1"
        android:orientation="horizontal"
        android:id="@+id/linearLayout"></LinearLayout>

</LinearLayout>

fragment_content.xml

<LinearLayout
    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:orientation="horizontal"
    android:background="@android:color/holo_purple">

    <TextView 
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="content"/>

</LinearLayout>

fragment_title.xml

<LinearLayout
    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:orientation="horizontal"
    android:background="@android:color/holo_orange_light">

    <TextView 
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="title"/>

</LinearLayout>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值