首页 > 其他分享 >android 动态添加 fragment

android 动态添加 fragment

时间:2022-08-27 14:25:05浏览次数:48  
标签:ft fragment inflater public 添加 android segment id

按钮点击触发:

    public void execute(View view) throws Exception {
FragmentManager fm = getFragmentManager();
FragmentTransaction ft = fm.beginTransaction();

    ft.add(R.id.fragmentContainer, new segment(), "HELLO");//R.id.fragmentContainer:占位
        ft.commit();
}
public class segment extends Fragment {
    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, Bundle savedInstanceState) {
        return inflater.inflate(R.layout.fragment,null);//R.layout.fragment:要模块的id
} }
<!--R.layout.fragment文件-->
<?xml version="1.0" encoding="utf-8"?> <androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" > <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/app_name" app:layout_constraintBottom_toBottomOf="parent" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintStart_toStartOf="parent" /> </androidx.constraintlayout.widget.ConstraintLayout>

<!--要显示fragment的文件-->
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_height="match_parent"
    android:layout_width="match_parent"
    xmlns:android="http://schemas.android.com/apk/res/android">

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:onClick="execute"
        android:text="execute"
        />
<!-- fragment的显示位置 -->
    <FrameLayout
        android:id="@+id/fragmentContainer"
        android:layout_width="match_parent"
        android:layout_height="match_parent"/>

</androidx.constraintlayout.widget.ConstraintLayout>

 

 


标签:ft,fragment,inflater,public,添加,android,segment,id
From: https://www.cnblogs.com/laremehpe/p/16630491.html

相关文章

  • Android AlarmManager
    publicclassMainActivityextendsAppCompatActivity{privatestaticfinalintNOTIFICATION_ID=0;AlarmManageralarmManager;IntentAlarmReceive......
  • Android Studio原生模拟器崩溃
    当你用AndroidStudio原生模拟器测试某个程序的时候模拟器突然就崩溃了如下图所示 当我们再次启动模拟器的时候提示我们已经有一个模拟器在启动中但是我们并没有看到呀......
  • fedora 添加 docker 源
    mv/etc/yum.repos.d/fedora.repo/etc/yum.repos.d/fedora.repo.backupwget-O/etc/yum.repos.d/fedora.repohttp://mirrors.aliyun.com/repo/fedora.repoyummakeca......
  • Atlassian Confluence 6.15.5 添加甘特图
     AtlassianConfluence编辑模式工具栏“+”→其它宏→视觉&图像→选择“路线图编辑器”打开“插入路线图计划器”弹出框。接下来就靠你发挥了。     成......
  • “X-Content-Type-Options”头缺失或不安全,添加Filter后 hsresponse.setHeader("X-Con
    AppScan扫描出的问题:  问题修复,添加Filter: 问题修复后,列表出不来: ......
  • VSCode添加自定义的Vue3.2标准模板
    创建新的用户片段 点击文件——首选项——用户片段或者用快捷Ctrl+Shift+P唤出控制台然后输入“snippets”并选择接着输入vuevscode自动生成vue.json文件{......
  • element-ui 在 el-table 标题中添加小图标
    效果如下:html代码:<divclass="table-boxcus-table-box"><el-table:data="ruleForm.tableData"borderstyle="width:100%"><el-table-columnprop="o......
  • 天地图添加多个覆盖物,点击切换选中icon
      天地图添加多个覆盖物,点击覆盖物,切换选中的icon,移除之前的icon,再次点击移除之前的。。。这个是react写的,先是确定中心位置,然后渲染点位,添加覆盖物,选中icon的不同, ......
  • MAUI Android Splash
    MAUI的Splash可以直接在Resources/Splash文件夹下直接更改,参阅官方文档https://docs.microsoft.com/zh-cn/dotnet/maui/user-interface/images/splashscreen?tabs=ios......
  • PowerDesigner添加索引(转载)
     原文链接:https://blog.csdn.net/a1720119024/article/details/90732860/生成的sql举例:createuniquenonclusteredindexIX_ZybwhysClass_NameonT_ZybwhysClass(N......