标签主机未显示标签 [英] Tab host not showing tabs

查看:125
本文介绍了标签主机未显示标签的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Tab Host来显示选项卡,但是即使我只是将其拖放到布局上并运行它,它也没有显示选项卡,而是显示白屏,我想这是第一个线性布局标签视图.

I am using tab host to show tabs, but even when I simply drag and drop it on my layout, and run it, its not showing the tabs, its showing white screen,I guess which is of the linear layout of first tab view.

<?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" >

    <TabHost
        android:id="@android:id/tabhost"
        android:layout_width="match_parent"
        android:layout_height="match_parent" >

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

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

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

                <LinearLayout
                    android:id="@+id/tab1"
                    android:layout_width="match_parent"
                    android:layout_height="match_parent" >
                </LinearLayout>

                <LinearLayout
                    android:id="@+id/tab2"
                    android:layout_width="match_parent"
                    android:layout_height="match_parent" >
                </LinearLayout>

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

</LinearLayout>

当我在模拟器或电话(Huawei Ascend P1)上运行时,没有显示任何选项卡.

When I run above on emulator or phone (Huawei Ascend P1), no tabs are shown.

推荐答案

之所以发生这种情况,仅仅是因为您不能仅使用xml代码创建TabHost.您需要像这样将TabSpec添加到TabHost:

This is happening simply because you just can't create TabHost using xml code only. You need to add TabSpecs to the TabHost like this:

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);

这篇关于标签主机未显示标签的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆