Android TabHost出现在片段前面

时间:2018-07-06 23:27:19

标签: android android-fragments android-tabhost

我无法在任何其他堆栈文章或Android文档中找到解决此问题的方法。由于某些原因,在使用support.v4库的FragmentActivity中,片段片段显示在tabhost的后面,如以下屏幕截图所示:

Issue with tabs

MainActivity.java:

public class MainActivity extends FragmentActivity {
    // Declare Variables
    private FragmentTabHost mTabHost;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // Set the view from main_fragment.xml
        setContentView(R.layout.main_fragment);

        setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT); //locks screen orientation to portrait       

        // Locate android.R.id.tabhost in main_fragment.xml
        mTabHost = (FragmentTabHost) findViewById(android.R.id.tabhost);

        // Create the tabs in main_fragment.xml
        mTabHost.setup(this, getSupportFragmentManager(), R.id.tabcontent);

        // Create Tab1 with a custom image in res folder
        mTabHost.addTab(mTabHost.newTabSpec("tab1").setIndicator("Tab 1"), 
                FragmentTab1.class, null);

        // Create Tab2
        mTabHost.addTab(mTabHost.newTabSpec("tab2").setIndicator("Tab 2"), 
                FragmentTab2.class, null);

        // Create Tab2
        mTabHost.addTab(mTabHost.newTabSpec("tab3").setIndicator("Tab 3"), 
                FragmentTab3.class, null);
        }

main_fragment.xml:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

    <android.support.v4.app.FragmentTabHost
        android:id="@android:id/tabhost"
        android:layout_width="match_parent"
        android:layout_height="match_parent" >

        <FrameLayout
            android:id="@+id/tabcontent"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
        />
    </android.support.v4.app.FragmentTabHost>

</LinearLayout>

有什么想法可以解决此问题并使片段的内容显示在选项卡下面吗?

谢谢

1 个答案:

答案 0 :(得分:1)

很简单:)您可以在[main_fragment.xml]中使用它:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

    <android.support.v4.app.FragmentTabHost
        android:id="@android:id/tabhost"
        android:layout_width="match_parent"
        android:layout_height="match_parent">

    </android.support.v4.app.FragmentTabHost>

    <FrameLayout
        android:id="@+id/tabcontent"
        android:layout_width="match_parent"
        android:layout_height="match_parent"/>

</LinearLayout>

或:[使用保证金]

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <android.support.v4.app.FragmentTabHost
        android:id="@android:id/tabhost"
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <RelativeLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent">

            <FrameLayout
                android:id="@+id/tabcontent"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:layout_marginTop="55dp"/>

        </RelativeLayout>

    </android.support.v4.app.FragmentTabHost>

</LinearLayout>

祝你好运。