我正在使用标签主机来显示标签,但即使我只是将它拖放到我的布局上并运行它,它也没有显示标签,而是显示白屏,我猜这是第一个的线性布局选项卡 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)上运行上面的代码时,没有显示标签。
最佳答案
发生这种情况仅仅是因为您无法仅使用 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/