TabHost仅显示一个内容

时间:2014-01-08 19:21:12

标签: android tabs

我是android开发的新手,我有以下xml布局和代码在TabActivity中添加标签,但选择的正确标签不显示。我有时改变了布局,但无法显示标签。

<TabHost xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@android:id/tabhost"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
tools:context=".MainActivity" >

<LinearLayout
    android:layout_width="600dp"
    android:layout_height="880dp"
    android:baselineAligned="false"
    android:orientation="vertical" >

    <LinearLayout
        android:layout_width="600dp"
        android:layout_height="840dp"
        android:layout_weight="1" >

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

            <FrameLayout
                android:id="@android:id/tabcontent"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content" >
            </FrameLayout>
        </TabWidget>
    </LinearLayout>

    <GridLayout
        android:layout_width="600dp"
        android:layout_height="40dp"
        android:layout_weight="1"
        android:columnCount="4"
        android:rowCount="1" >

        <ImageButton
            android:id="@+id/button_scan"
            style="android:attr/buttonBarButtonStyle"
            android:layout_width="150dp"
            android:layout_height="wrap_content"
            android:layout_column="0"
            android:layout_row="0"
            android:onClick="doScan"
            android:src="@drawable/lupa" />

        <ImageButton
            android:id="@+id/button_foto"
            style="android:attr/buttonBarButtonStyle"
            android:layout_width="150dp"
            android:layout_height="wrap_content"
            android:layout_column="1"
            android:layout_row="0"
            android:src="@drawable/camera" />

        <ImageButton
            android:id="@+id/button_pinteresses"
            style="android:attr/buttonBarButtonStyle"
            android:layout_width="150dp"
            android:layout_height="wrap_content"
            android:layout_column="2"
            android:layout_row="0"
            android:src="@drawable/globe" />

        <ImageButton
            android:id="@+id/button_help"
            style="android:attr/buttonBarButtonStyle"
            android:layout_width="150dp"
            android:layout_height="wrap_content"
            android:layout_column="3"
            android:layout_row="0"
            android:src="@drawable/question" />
    </GridLayout>
</LinearLayout>

</TabHost>

在TabActivity中:

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    TabHost tabHost;
    tabHost = getTabHost();
    tabHost.setVisibility(TabHost.VISIBLE);

    TabSpec tab1 = tabHost.newTabSpec("TabIndoor");
    TabSpec tab2 = tabHost.newTabSpec("TabOutdoor");

    tab1.setIndicator("Indoor");
    tab1.setContent(new Intent(this, com.paar.ch9.MainActivity.class));

    tab2.setIndicator("Outdoor");
    tab2.setContent(new Intent(this, com.paar.ch9.MainActivity.class));

    tabHost.addTab(tab1);
    tabHost.addTab(tab2);
}

如何让标签可见?

1 个答案:

答案 0 :(得分:0)

你也需要使用TabSpec - 我没有时间测试它,但是在你的java代码上,试试这个并实现onTabChangeListener:

public class Main extends TabActivity implements OnTabChangeListener {

TabHost tabHost;

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    setContentView(R.layout.activity_main);

    tabHost = getTabHost();
    tabHost.getTabWidget().setStripEnabled(false);
    tabHost.setOnTabChangedListener((OnTabChangeListener) this);

    TabSpec spec1 = tabHost.newTabSpec("Indoor");
    //below sets the text and image on the tab
    spec1.setIndicator("", getResources().getDrawable(R.drawable.indoorImage));
    Intent intIndoor = new Intent(this, Indoor.class);
    spec1.setContent(intIndoor);

    TabSpec spec2 = tabHost.newTabSpec("Outdoor");
    //below sets the text and image on the tab
    spec2.setIndicator("Outdoor", getResources().getDrawable(R.drawable.outdoorImage));
    Intent intOutdoor = new Intent(this, Outdoor.class);
    spec2.setContent(intOutdoor);

    tabHost.addTab(spec1);
    tabHost.addTab(spec2);
}

你的xml代码可以很简单:

    <?xml version="1.0" encoding="utf-8"?>
    <TabHost
    android:id="@android:id/tabhost"
    android:layout_height="fill_parent"
    android:layout_width="fill_parent"
    xmlns:android="http://schemas.android.com/apk/res/android" >

    <TabWidget
        android:id="@android:id/tabs"
        android:layout_width="fill_parent"
        android:layout_height="50dp" >
    </TabWidget>

    <FrameLayout
        android:id="@android:id/tabcontent"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent" >
    </FrameLayout>

    </TabHost>