android – 如何动态更改片段的类

android – 如何动态更改片段的类,第1张

概述嗨我有一个包含两个片段的linearLayout,我在这个布局中添加了代码选项卡.我想要的是当我点击tab1时可以从指定的类中填充自己的片段,但是在tab2中我想将这个类更改为另一个类.谢谢 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="horizo 嗨我有一个包含两个片段的linearLayout,我在这个布局中添加了代码选项卡.我想要的是当我点击tab1时可以从指定的类中填充自己的片段,但是在tab2中我想将这个类更改为另一个类.谢谢
<linearLayout xmlns:androID="http://schemas.androID.com/apk/res/androID"    androID:orIEntation="horizontal"    androID:layout_wIDth="match_parent"    androID:layout_height="match_parent"    androID:ID="@+ID/frags">   <fragment             androID:ID="@+ID/frag_Title"            androID:visibility="gone"            androID:layout_margintop="?androID:attr/actionbarSize"            androID:layout_wIDth="@dimen/Titles_size"            androID:layout_height="match_parent" />     <fragment             androID:ID="@+ID/frag_content"            androID:layout_wIDth="match_parent"            androID:layout_height="match_parent" />
解决方法 更改< fragment />在xml布局中< FrameLayout />
<FrameLayout        androID:ID="@+ID/frag_Title"        androID:visibility="gone"        androID:layout_margintop="?androID:attr/actionbarSize"        androID:layout_wIDth="@dimen/Titles_size"        androID:layout_height="match_parent" /> <FrameLayout        androID:ID="@+ID/frag_content"        androID:layout_wIDth="match_parent"        androID:layout_height="match_parent" />

并以编程方式添加片段:

FragmentManager fragmentManager = getFragmentManager()FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();ExampleFragment fragment = new ExampleFragment();fragmentTransaction.replace(R.ID.frag_content,fragment);fragmentTransaction.commit();

但最初阅读this.

总结

以上是内存溢出为你收集整理的android – 如何动态更改片段的类全部内容,希望文章能够帮你解决android – 如何动态更改片段的类所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址:https://www.54852.com/web/1134919.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2022-05-30
下一篇2022-05-30

发表评论

登录后才能评论

评论列表(0条)

    保存