Fragment

 /**
     * 片段可以重叠
     * 添加片段
     */
    public void add(View view) {
        //
        FragmentManager fm = getSupportFragmentManager();
        FragmentTransaction ft = fm.beginTransaction();
        ft.add(R.id.fragment, new RedFragment());
        ft.commit();
    }

    /**
     * 删除当前布局上的片段
     */
    public void del(View view) {
        FragmentManager fm = getSupportFragmentManager();
        // 获取当前的片段对象
        Fragment currentFragment = fm.findFragmentById(R.id.fragment);
        if (currentFragment != null) {
            FragmentTransaction ft = fm.beginTransaction();
            ft.remove(currentFragment);
            ft.commit();
        }
    }

    /**
     * 不管之前后没有都会替换掉
     */
    public void replace(View view) {
        FragmentManager fm = getSupportFragmentManager();
        FragmentTransaction ft = fm.beginTransaction();
        ft.replace(R.id.fragment, new BlueFragment());
        ft.commit();
    }

    /**
     * 添加到回退栈
     */
    public void toBack(View view) {
        FragmentManager fm = getSupportFragmentManager();
        FragmentTransaction ft = fm.beginTransaction();

        ft.replace(R.id.fragment, new RedFragment());
        ft.addToBackStack(null);
        ft.commit();
    }
<?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_main"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.example.zxc.myapplication.MainActivity">
<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal">
        <Button
            android:onClick="add"
            android:text="添加"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/btn_add"/>
        <Button
            android:onClick="del"
            android:text="删除"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/btn_del"/>
        <Button
            android:onClick="replace"
            android:text="替换"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/btn_rep"/>
        <Button
            android:onClick="toBack"
            android:text="添加到回退栈"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/btn_toback"/>

    </LinearLayout>

    <FrameLayout
        android:id="@+id/fragment"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1"></FrameLayout>
</LinearLayout>

</RelativeLayout>

这里写图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值