[英]Change Fragment destroy my Layout
有人可以为我提供以下帮助吗:我只想单击一下按钮,中间的片段会发生变化:
但是,如果我单击一个,则会发生以下情况:
谁知道,怎么了?
我的片段只是带有文本框的简单LineaLayouts。 我的Main.xml看起来像这样:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/fragmentholder"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<View
android:id="@+id/actionbar"
android:layout_width="match_parent"
android:layout_height="60dp"
android:background="@drawable/actionbar_design" />
<LinearLayout
android:id="@+id/fragments"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:background="@color/fragment_background"
android:orientation="vertical" >
<fragment
android:id="@+id/clubFragment"
android:name="com.ivenda.clubs.ClubListFragment"
android:layout_width="match_parent"
android:layout_height="match_parent" />
<fragment
android:id="@+id/ticketFragment"
android:name="com.ivenda.tickets.TicketListFragment"
android:layout_width="match_parent"
android:layout_height="match_parent" />
<fragment
android:id="@+id/chatFragment"
android:name="com.ivenda.chat.ChatListFragment"
android:layout_width="match_parent"
android:layout_height="match_parent" />
<fragment
android:id="@+id/settingsFragment"
android:name="com.ivenda.settings.SettingsFragment"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</LinearLayout>
<LinearLayout
android:id="@+id/tabbar"
android:layout_width="match_parent"
android:layout_height="60dp"
android:background="@drawable/tabbar_design"
android:orientation="horizontal"
android:gravity="bottom">
<LinearLayout
android:id="@+id/tabbarCustom1"
android:layout_width="match_parent"
android:layout_height="60dp"
android:layout_weight="1"
android:background="@drawable/tabbar_design"
android:orientation="horizontal" >
<ImageButton
android:id="@+id/tabBarChat"
android:layout_width="wrap_content"
android:layout_height="60dp"
android:layout_marginRight="1dp"
android:layout_weight="1"
android:background="@drawable/tabbar_background_selector"
android:src="@drawable/ic_launcher" />
<View
android:id="@+id/View02"
android:layout_width="2dp"
android:layout_height="40dp"
android:layout_gravity="center"
android:background="@color/tabbar_divider" />
<ImageButton
android:id="@+id/tabBarClubs"
android:layout_width="wrap_content"
android:layout_height="60dp"
android:layout_weight="1"
android:layout_marginRight="1dp"
android:background="@drawable/tabbar_background_selector"
android:src="@drawable/ic_launcher" />
</LinearLayout>
<View
android:id="@+id/View03"
android:layout_width="2dp"
android:layout_height="40dp"
android:layout_gravity="center"
android:background="@color/tabbar_divider" />
<LinearLayout
android:id="@+id/tabbarCustom2"
android:layout_width="match_parent"
android:layout_height="60dp"
android:layout_weight="1"
android:background="@drawable/tabbar_design"
android:orientation="horizontal" >
<ImageButton
android:id="@+id/tabBarTickets"
android:layout_width="wrap_content"
android:layout_height="60dp"
android:layout_marginRight="1dp"
android:layout_weight="1"
android:background="@drawable/tabbar_background_selector"
android:src="@drawable/ic_launcher" />
<View
android:id="@+id/View01"
android:layout_width="2dp"
android:layout_height="40dp"
android:layout_gravity="center"
android:background="@color/tabbar_divider" />
<ImageButton
android:id="@+id/tabBarSettings"
android:layout_width="wrap_content"
android:layout_height="60dp"
android:layout_weight="1"
android:background="@drawable/tabbar_background_selector"
android:src="@drawable/ic_launcher" />
</LinearLayout>
</LinearLayout>
要更改片段,我使用此调用:
switchFragment(R.id.fragmentholder, new SettingsFragment());
private void switchFragment(int fragId, Fragment frag){
FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
ft.replace(fragId, frag);
ft.commit();
}
非常感谢!!
如果只想一次显示其中一个片段,请不要将所有四个片段放在布局中。 只需将ID为"@+id/fragmentholder"
的FrameLayout
放在要显示片段的位置的布局中,然后调用您的方法,即可用所需的片段替换FrameLayout
的内容。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.