TabWidget will not display, even though it displays in ADT editor

柔情痞子 提交于 2019-12-24 03:16:45

问题


My TabWidget will not display, even though it shows up in the Eclipse graphical editor. I can't find any reason why. Why isn't my tab bar showing?

Eclipse

Emulator

XML source of the activity: http://pastebin.com/Au9XFXPa

Extract from activity:

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

Results from Android lint:

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

回答1:


It's hard to tell without actual code of how your activity is implemented, but seems like you need to call setContent() on your 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);

This should give you the idea.




回答2:


Try using Fragements inside TabHost, something like this:

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

Then initialize your tabs, by doing something like this:

/**
 * 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);
}


来源:https://stackoverflow.com/questions/16661420/tabwidget-will-not-display-even-though-it-displays-in-adt-editor

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!