【问题标题】:Android TabHost appears in Front of FragmentsAndroid TabHost 出现在 Fragments 的前面
【发布时间】:2018-07-06 23:27:19
【问题描述】:

我无法在任何其他堆栈帖子或 Android 文档中找到此问题的解决方案。出于某种原因,在使用 support.v4 库的 FragmentActivity 中,片段出现在 tabhost 后面,如下图所示:

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>

任何想法如何解决此问题并使片段的内容显示在选项卡下方?

谢谢

【问题讨论】:

    标签: android android-fragments android-tabhost


    【解决方案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>
    

    祝你好运。

    【讨论】:

    • 您能否评论一下您所做的更改以及它们为何相关?这将有助于 OP 和下一位读者更好地理解您的答案
    猜你喜欢
    • 2013-07-15
    • 2013-06-18
    • 1970-01-01
    • 2012-12-27
    • 2015-07-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多