android - 选项卡主机不显示选项卡

标签 android tabs android-tabhost

我正在使用标签主机来显示标签,但即使我只是将它拖放到我的布局上并运行它,它也没有显示标签,而是显示白屏,我猜这是第一个的线性布局选项卡 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)上运行上面的代码时,没有显示标签。

Screen shot

最佳答案

发生这种情况仅仅是因为您无法仅使用 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);

关于android - 选项卡主机不显示选项卡,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17018660/

相关文章:

c# - 多个选项卡中的 MVC 表单验证 - 自动跳转到有验证错误的选项卡?

java - 选择选项卡时在选项卡中加载 Google map ,而不是从头开始

java - 我无法在 Android 应用程序中正确加载包含 TabHost 的 xml

android - 从 cocos2dx 调用 android runOnUiThread 可能会使 menutitem 闪烁

android - VerticalSeekBar 和事件

android - 如何在微调器中按字母顺序对数据进行排序

选中和取消选中时Android更改选项卡栏文本颜色

java - 在 java 中向 .json URI 发出获取请求并接收其内容

javascript - JQuery 选项卡 : Can the tabs and content be in different containers?

android - 是否可以在线下载 Activity 并将其加载到 Android 的选项卡中?