Tabhost未显示选项卡

时间:2015-11-02 13:19:59

标签: java android

我按照在互联网上找到的教程,但似乎没有工作......

我总是打开Tab 1,没关系,但是我没有看到TABS菜单......

这是我的代码:

Main2Activity:

public class Main2Activity  extends Activity
{
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main2);

        // create the TabHost that will contain the Tabs
        TabHost tabs=(TabHost)findViewById(R.id.tabhost);

        tabs.setup();

        TabHost.TabSpec spec=tabs.newTabSpec("tag1");

        spec.setContent(R.id.tab1);
        spec.setIndicator("Analog Clock");
        tabs.addTab(spec);

        spec=tabs.newTabSpec("tag2");
        spec.setContent(R.id.tab2);
        spec.setIndicator("DigitalClock");
        tabs.addTab(spec);

        spec=tabs.newTabSpec("tag3");
        spec.setContent(R.id.tab3);
        spec.setIndicator("Button");
        tabs.addTab(spec);
    }

Content_main2.xml:

<?xml version="1.0" encoding="utf-8"?>
<TabHost xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+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"/>
        <FrameLayout android:id="@android:id/tabcontent"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent">
            <AnalogClock android:id="@+id/tab1"
                android:layout_width="fill_parent"
                android:layout_height="fill_parent"/>
            <DigitalClock android:id="@+id/tab2"
                android:layout_width="fill_parent"
                android:layout_height="fill_parent"/>
            <Button android:id="@+id/tab3"
                android:layout_width="fill_parent"
                android:layout_height="fill_parent"
                android:text="Do Nothing"/>
        </FrameLayout>
    </LinearLayout>
</TabHost>

This is how it looks when i run it

2 个答案:

答案 0 :(得分:1)

我解决了它:

setContentView(R.layout.content_main2);

我正在调用activity_main2,但不知道为什么当我调用activity_main2时没有得到content_main2。

我在Activity_main2.xml中:

<include layout="@layout/content_main2" />

有谁知道为什么?

答案 1 :(得分:0)

您可以seperate activity使用each tab。正如“intelliJ”所说。为每个seperate activity创建seperate xmltab文件,然后分别在每个活动中添加模拟数字和按钮。请在activity1中添加analog,然后在activity2中添加digital,依此类推。