TabWidget将不会显示,即使它显示在ADT编辑器中

时间:2013-05-21 02:54:08

标签: android android-layout layout android-tabhost

即使它出现在Eclipse图形编辑器中,我的TabWidget也不会显示。我找不到任何理由。为什么我的标签栏没有显示?

的Eclipse

Imgur

仿真器

Imgur

活动的XML来源:http://pastebin.com/Au9XFXPa

从活动中提取:

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

Android lint的结果:

$ lint.bat res/layout/activity_calculator.xml
Scanning Catalyst: .
No issues found.

2 个答案:

答案 0 :(得分:5)

如果没有实际的代码表示您的活动是如何实施的话,很难说,但您似乎需要在setContent()上致电TabHost

    TabHost tabs = (TabHost)findViewById(R.id.tabhost);
    tabs.setup();

    // Calculator
    TabHost.TabSpec calculatorTab = tabs.newTabSpec("calculator");
    calculatorTab.setContent(R.id.calculator);
    calculatorTab.setIndicator("Calculator");
    tabs.addTab(calculatorTab);

    // Home
    TabHost.TabSpec homeTab = tabs.newTabSpec("home");
    homeTab.setContent(R.id.home);
    tabs.addTab(homeTab);

    // Home
    TabHost.TabSpec faqTab = tabs.newTabSpec("faq");
    faqTab.setContent(R.id.faq);
    tabs.addTab(faqTab);

这应该会给你一个想法。

答案 1 :(得分:0)

尝试在Fragements内使用TabHost,如下所示:

 <TabHost
    android:id="@android:id/tabhost"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent" >

    <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:orientation="vertical" >

        <TabWidget
            android:id="@android:id/tabs"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_weight="0"
            android:orientation="horizontal" />

        <FrameLayout
            android:id="@android:id/tabcontent"
            android:layout_width="0dp"
            android:layout_height="0dp"
            android:layout_weight="0" />

        <android.support.v4.view.ViewPager
            android:id="@+id/viewpager"
            android:layout_width="fill_parent"
            android:layout_height="0dp"
            android:layout_weight="1" />
    </LinearLayout>
</TabHost>

然后通过执行以下操作来初始化标签:

/**
 * Initialise the Tab Host
 */
private void initialiseTabHost(Bundle args) {
    mTabHost = (TabHost) findViewById(android.R.id.tabhost);
    mTabHost.setup();
    TabInfo tabInfo = null;
    RedeemActivity.AddTab(this, this.mTabHost,
            this.mTabHost.newTabSpec("Tab1").setIndicator("1"),
            (tabInfo = new TabInfo("Tab1", Activity1.class, args)));
    this.mapTabInfo.put(tabInfo.tag, tabInfo);
    RedeemActivity.AddTab(this, this.mTabHost,
            this.mTabHost.newTabSpec("Tab2").setIndicator("2"),
            (tabInfo = new TabInfo("Tab2", Activity2.class, args)));
    this.mapTabInfo.put(tabInfo.tag, tabInfo);
    RedeemActivity.AddTab(this, this.mTabHost,
            this.mTabHost.newTabSpec("Tab3").setIndicator("3"),
            (tabInfo = new TabInfo("Tab3", Activity3.class, args)));
    this.mapTabInfo.put(tabInfo.tag, tabInfo);
    // Default to first tab
    // this.onTabChanged("Tab1");
    //
    mTabHost.setOnTabChangedListener(this);
}