我有一个 TabHost
,我想动态创建标签。我看过很多例子,但它们没有用。
TabHost tabHost;
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
tabHost = (TabHost)findViewById(android.R.id.tabhost);
tabHost.setup();
TabHost.TabSpec tab1 = tabHost.newTabSpec("First Tab");
tab1.setIndicator("First Tab");
tab1.setContent(new Intent(this,MainActivity2.class));
tabHost.addTab(tab1);
这是我尝试过的最后一件事,但它不起作用。这是我考虑过的一些链接。我找不到我做错了什么。
<TabHost xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.LH.tabletres.MainActivity">
<LinearLayout
android:id="@+id/LinearLayout01"
android:orientation="vertical"
android:layout_height="fill_parent"
android:layout_width="fill_parent">
<TabWidget
android:id="@android:id/tabs"
android:layout_height="wrap_content"
android:layout_width="fill_parent">
</TabWidget>
<FrameLayout
android:id="@android:id/tabcontent"
android:layout_height="fill_parent"
android:layout_width="fill_parent">
</FrameLayout>
</LinearLayout>
</TabHost>
最佳答案
我想你忘了将 android:id="@android:id/tabhost"
设置为你的 tabhost
。如下图
<TabHost xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@android:id/tabhost"
android:layout_width="match_parent"
android:layout_height="match_parent" >........
.....
</TabHost>
关于android - TabHost 动态添加标签页,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23469998/