标签不会显示

时间:2014-09-28 10:46:38

标签: android android-tabhost tabactivity

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

输出是: enter image description here

无法加载两个标签。 请说明我犯了错误的地方。?

2 个答案:

答案 0 :(得分:0)

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);

答案 1 :(得分:0)

对于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部分,您必须删除</TabWidget>并更改:

      <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"/>
相关问题