0
添加tabhost和tabcontent后无法创建tabview。 如何在android中添加选项卡。我使用下面的代码来显示选项卡。不显示标签
XML文件:
<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:id="@+id/homeLinearLayout"
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" >
<FrameLayout
android:id="@android:id/tabcontent"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<LinearLayout
android:id="@+id/dynamicLinearLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Dynamic" />
</LinearLayout>
<LinearLayout
android:id="@+id/staticLinearLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<Button
android:id="@+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Static" />
</LinearLayout>
</FrameLayout>
</TabWidget>
</LinearLayout>
</TabHost>
</LinearLayout>
Java文件
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_home_view);
tabHost = (TabHost) findViewById(android.R.id.tabhost);
tabHost.setup();
tabSpec = tabHost.newTabSpec("dynamicTab");
tabSpec.setIndicator("Create Image");
tabSpec.setContent(R.id.dynamicLinearLayout);
tabHost.addTab(tabSpec);
tabSpec = tabHost.newTabSpec("staticTab");
tabSpec.setIndicator("Select Image");
tabSpec.setContent(R.id.staticLinearLayout);
tabHost.addTab(tabSpec);
tabHost.setCurrentTab(0);
}
输出是:
无法加载两个标签。 请显示我犯的错误。
你需要给一个不同的名字ta来自不同的前一个像这个tabSpec2的bhost – sakir 2014-09-28 12:48:44