动态管理片段
来源:互联网 发布:淘宝彩票走势图 编辑:程序博客网 时间:2024/04/30 22:27
动态管理片段
/** * 片段可以重叠 * 添加片段 */ 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
- 动态管理片段
- Mybatis动态sql___sql片段
- 动态SQL片段
- 动态创建style片段和script片段
- 中国式管理精彩片段【曾仕强】
- 动态增删表格中的行【代码片段】
- 动态加载jquery(代码片段)
- [mybatis]动态SQL与SQL片段
- Mybatis动态sql和sql片段
- Mybatis_01_理解动态sql及sql片段
- Java程序片段 之动态生成表格
- 使用jquery实现动态加载片段
- C# Snippet使用,代码片段管理
- 片段
- 片段
- 片段
- 片段
- 动态修改下拉列表的内容(代码片段)
- Linux下使用inotify监控文件动作
- Ajax跨域请求
- 都说微商不好做!有谁考虑用户最终目的过?
- js用正则进行密码强度验证
- 网站SEO优化问答精选
- 动态管理片段
- 如何给RadioButton设置图片大小
- 在使用模板类的时候注意事项
- Kendo UI grid API 获取 filter 后的所有数据
- PHP空间出现session无法保存问题解决办法
- Python多进程通信Queue、Pipe、Value、Array实例
- spring的AOP基础、前置通知与后置通知
- C语言简单递归实现字符串逆序输出
- 全国雾霾大数据,哪里才是桃花源?