嗨我有一个包含两个片段的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" />
并以编程方式添加片段:
- FragmentManager fragmentManager = getFragmentManager()
- FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
- ExampleFragment fragment = new ExampleFragment();
- fragmentTransaction.replace(R.id.frag_content,fragment);
- fragmentTransaction.commit();
但最初阅读this.