【问题标题】:TabHost not showing in the screenTabHost 未显示在屏幕中
【发布时间】:2015-01-16 05:48:44
【问题描述】:

我试图在我的应用程序上使用TabHost,我只是使用设计将它拖到我的活动中,但是当我运行它时,它就不会出现,只是得到白屏,有人知道吗为什么?

<TabHost
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:id="@+id/tabHost"
    android:layout_gravity="center_horizontal">

    <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:orientation="vertical">

        <TabWidget
            android:id="@android:id/tabs"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"></TabWidget>

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

            <LinearLayout
                android:id="@+id/tab1"
                android:layout_width="fill_parent"
                android:layout_height="fill_parent"
                android:orientation="vertical"></LinearLayout>

            <LinearLayout
                android:id="@+id/tab2"
                android:layout_width="fill_parent"
                android:layout_height="fill_parent"
                android:orientation="vertical"></LinearLayout>

            <LinearLayout
                android:id="@+id/tab3"
                android:layout_width="fill_parent"
                android:layout_height="fill_parent"
                android:orientation="vertical"></LinearLayout>
        </FrameLayout>
    </LinearLayout>
</TabHost>

【问题讨论】:

    标签: android layout android-tabhost


    【解决方案1】:

    发生这种情况仅仅是因为您不能仅使用 XML 代码创建 TabHost。您需要像这样将TabSpecs 添加到TabHost

    TabHost tabHost = (TabHost)findViewById(android.R.id.tabhost);
    
    TabSpec tab1 = tabHost.newTabSpec("First Tab");
    TabSpec tab2 = tabHost.newTabSpec("Second Tab");
    TabSpec tab3 = tabHost.newTabSpec("Third Tab");
    
    tab1.setIndicator("Tab1");
    tab1.setContent(new Intent(this,TabActivity1.class));
    
    tab2.setIndicator("Tab2");
    tab2.setContent(new Intent(this,TabActivity2.class));
    
    tab3.setIndicator("Tab3");
    tab3.setContent(new Intent(this,TabActivity3.class));
    
    tabHost.addTab(tab1);
    tabHost.addTab(tab2);
    tabHost.addTab(tab3);
    

    【讨论】:

    • “setContent(new Intent(this,TabActivity2.class))”方法是干什么用的?我也是这样做的,但是应用程序抛出了 NullPointerException,所以我最终将它替换为 setContent(R.id.yourTabId);
    • 在此示例中,我将活动放在选项卡中,因此您需要在项目中创建 TabActivity1,2,3
    • 你忘记了 tabHost.setup() (我的情况有一个例外)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-08-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多