【问题标题】:Tabs are not displayed选项卡不显示
【发布时间】:2014-09-28 10:46:37
【问题描述】:

添加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);
}

输出是:

无法加载两个标签。 请指出我在哪里做错了。?

【问题讨论】:

  • 你需要给 tabhost 一个不同于之前的名字的名字,比如这个 tabSpec2

标签: android android-tabhost tabactivity


【解决方案1】:
tabSpec = tabHost.newTabSpec("dynamicTab");
    tabSpec.setIndicator("Create Image");
    tabSpec.setContent(R.id.dynamicLinearLayout);
    tabHost.addTab(tabSpec);

    tabSpec2 = tabHost.newTabSpec("staticTab");
    tabSpec2.setIndicator("Select Image");
    tabSpec2.setContent(R.id.staticLinearLayout);
    tabHost2.addTab(tabSpec2);

    tabHost.setCurrentTab(0);

【讨论】:

    【解决方案2】:

    对于 java 部分,sakir 的答案是正确的,您可以使用它或使用我的:

        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        TabHost tabHost = (TabHost) findViewById(android.R.id.tabhost);
        tabHost.setup();
    
        TabHost.TabSpec tabSpec = tabHost.newTabSpec("dynamicTab");
        tabSpec.setIndicator("Create Image");
        tabSpec.setContent(R.id.dynamicLinearLayout);
        tabHost.addTab(tabSpec);
    
        TabHost.TabSpec tabSpec1 = tabHost.newTabSpec("staticTab");
        tabSpec1.setIndicator("Select Image");
        tabSpec1.setContent(R.id.staticLinearLayout);
        tabHost.addTab(tabSpec1);
    
        tabHost.setCurrentTab(0); 
    

    对于您的 xml 部分,您必须删除 &lt;/TabWidget&gt; 并更改:

          <TabWidget
                android:id="@android:id/tabs"
                android:layout_width="match_parent"
                android:layout_height="wrap_content" >
    

           <TabWidget
                android:id="@android:id/tabs"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"/>
    

    【讨论】: