我有一个包含 TabHost 的 XML 文件。我是直接右键布局图->新建->Android XML->TabHost创建的。代码似乎没问题,但每当我在 Eclipse 中进行图形布局时,它都会给我错误(见标题)。我正在使用 Android 2.2。这是我的 XML 代码:
<?xml version="1.0" encoding="utf-8"?>
<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" >
<LinearLayout
android:id="@+id/linearLayout1"
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>
最佳答案
我的解决方案是为每个选项卡的 View 指定 id。在下面的示例中,需要为每个 textView 提供 id(如 android:id="@+id/tab1"...)
<?xml version="1.0" encoding="utf-8"?>
<TabHost xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@android:id/tabhost"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<LinearLayout
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<TabWidget
android:id="@android:id/tabs"
android:layout_width="fill_parent"
android:layout_height="wrap_content">
<TextView
android:tag="tab1"
android:text="@string/tab1"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:textSize="12sp"
/>
<TextView
android:tag="tab2"
android:text="@string/tab2"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:textSize="12sp"
/>
<TextView
android:tag="tab3"
android:text="@string/tab3"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:textSize="12sp"
/>
<TextView
android:tag="tab4"
android:text="@string/tab4"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:textSize="12sp"
/>
<TextView
android:tag="tab5"
android:text="@string/tab5"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:textSize="12sp"
/>
</TabWidget>
<FrameLayout
android:id="@android:id/tabcontent"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<TextView
android:id="@+id/tab1"
android:text="Hallo3"
android:layout_width="fill_parent"
android:layout_height="fill_parent" />
<TextView
android:id="@+id/tab2"
android:text="Hallo2"
android:layout_width="fill_parent"
android:layout_height="fill_parent" />
<TextView
android:id="@+id/tab3"
android:text="Hallo3"
android:layout_width="fill_parent"
android:layout_height="fill_parent" />
<TextView
android:id="@+id/tab4"
android:text="Hallo3"
android:layout_width="fill_parent"
android:layout_height="fill_parent" />
<TextView
android:id="@+id/tab5"
android:text="Hallo3"
android:layout_width="fill_parent"
android:layout_height="fill_parent" />
</FrameLayout>
</LinearLayout>
</TabHost>
关于android - 无法创建选项卡内容,因为在 Android Eclipse 中找不到 ID 为 -1 的 View XML 错误?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12338023/