我在一個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