我在一个FragmentActivity内使用一个FragmentTabHost,它有3个选项卡。 如果我改变从每个选项卡的第一页选项卡它的工作正常,但如果我导航抛出片段内一个选项卡,然后更改选项卡,它显示新的片段重叠到旧的。 有人可以帮助我吗?FragmentTabHost显示片段重叠
这是FragmentActivity(fragments_tab.xml)的布局:
<?xml version="1.0" encoding="UTF-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/relativeLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:src="@drawable/background_gradient">
<ImageView
android:contentDescription="@string/app_name"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:src="@drawable/background_gradient" />
<android.support.v4.app.FragmentTabHost
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@android:id/tabhost"
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TabWidget
android:id="@android:id/tabs"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="0"/>
<FrameLayout
android:id="@android:id/tabcontent"
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_weight="0"/>
<LinearLayout
android:id="@+id/realtabcontent"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"/>
</LinearLayout>
</android.support.v4.app.FragmentTabHost>
</RelativeLayout>
我FragmentActivity代码:
public class MainActivity extends FragmentActivity {
private FragmentTabHost mTabHost;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setDB();
setContentView(R.layout.fragments_tab);
mTabHost = (FragmentTabHost)findViewById(android.R.id.tabhost);
mTabHost.setup(this, getSupportFragmentManager(), R.id.realtabcontent);
mTabHost.addTab(mTabHost.newTabSpec("Home").setIndicator("Home", getResources().getDrawable(R.drawable.ic_menu_home)),
HomeFragment.class, null);
mTabHost.addTab(mTabHost.newTabSpec("Map").setIndicator("Map", getResources().getDrawable(R.drawable.ic_menu_mapmode)),
MapMenuFragment.class, null);
mTabHost.addTab(mTabHost.newTabSpec("Info").setIndicator("Info", getResources().getDrawable(R.drawable.ic_menu_info_details)),
InfoFragment.class, null);
}
}
每个片段创建这样:
public class InfoFragment extends Fragment {
@Override
public void onActivityCreated(Bundle savedInstanceState)
{
super.onActivityCreated(savedInstanceState);
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
{
View view = inflater.inflate(R.layout.fragment_info, container, false);
return view;
}
有人能够帮助我好吗?
谢谢回复@WingedRayeth,但我认为您的解决方案,你会每次你改变标签的时间清理堆栈。因此,例如,如果您导航到第一个选项卡直到第四个片段,然后更改选项卡,那么当您返回到第一个选项卡时,您将从第一个片段重新启动。 我终于找到解决办法,我会在下面解释 – al1812 2013-03-02 10:24:45