问题
Is it possible to define a tab layout only within the layout xml file? Everytime I find a tabhost tutorial it only works with some additional java code.
Here is my test.xml which don't display any tabs:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:id="@+id/linearLayout">
<TextView android:text="@string/app_name" android:layout_height="wrap_content" android:layout_width="wrap_content" android:id="@+id/textView1" android:textAppearance="?android:attr/textAppearanceLarge"></TextView>
<TabHost android:id="@android:id/tabhost" android:layout_width="match_parent" android:layout_height="match_parent">
<TabWidget android:layout_width="match_parent" android:layout_height="wrap_content" android:id="@android:id/tabs">
</TabWidget>
<FrameLayout android:layout_width="match_parent" android:layout_height="match_parent" android:id="@android:id/tabcontent">
<LinearLayout android:layout_width="match_parent" android:layout_height="match_parent" android:id="@+id/tab1">
<TextView android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="TAB 1"/>
</LinearLayout>
<LinearLayout android:layout_width="match_parent" android:layout_height="match_parent" android:id="@+id/tab2">
<TextView android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="TAB 2"/>
</LinearLayout>
</FrameLayout>
</TabHost>
</LinearLayout>
回答1:
Its not possible, as TabSpec is the holder class which represents each tab in TabHost. and there is no TabSpec xml equivalent tags specified to be put in xml layout.
Even through code, for a TabSpec, an indicator and content both have to be specified. Otherwise it will throw either
IllegalArgumentException: you must specify a way to create the tab content
or
IllegalArgumentException: you must specify a way to create the tab indicator.
来源:https://stackoverflow.com/questions/6996474/define-tabhost-tabwidget-content-within-the-xml-file-without-any-code-is-tha