gpt4 book ai didi

android - 如何动态更改 fragment 的类

转载 作者:塔克拉玛干 更新时间:2023-11-02 19:07:21 25 4
gpt4 key购买 nike

您好,我有一个包含两个 fragment 的 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 class="com.tugce.MitsActionBar.DoktorlarFragment"
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 class="com.tugce.MitsActionBar.ContentFragment"
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" />

并以编程方式添加 fragment :

FragmentManager fragmentManager = getFragmentManager()
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();

ExampleFragment fragment = new ExampleFragment();
fragmentTransaction.replace(R.id.frag_content, fragment);
fragmentTransaction.commit();

但首先阅读this .

关于android - 如何动态更改 fragment 的类,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7431516/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com