【问题标题】:TabWidget will not display, even though it displays in ADT editorTabWidget 不会显示,即使它显示在 ADT 编辑器中
【发布时间】:2013-05-21 02:54:08
【问题描述】:

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

日食

模拟器

活动的 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.

【问题讨论】:

  • 您在代码中的哪个位置包含activity_calculator.xml
  • 我推荐使用 Fragment 而不是 Tab Widget :D
  • @Jeff Lee。它适用于 Android 版本 8+。版本 11 以下不包含 Fragments。大多数手机仍然是 2.3,即 API 10。
  • @shoerat 它作为活动的一部分自动包含在 onCreate() setContentView(R.layout.activity_calculator);

标签: android android-layout layout android-tabhost


【解决方案1】:

如果没有实际代码来说明您的活动是如何实现的,很难判断,但您似乎需要在您的 TabHost 上调用 setContent()

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

这应该会给你这个想法。

【讨论】:

  • 谢谢。我很困惑。我认为它就像其他小部件或 GWT 图形编辑器一样,不需要编程。我清理了答案(tabhost,setIndicator)。
【解决方案2】:

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

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

【讨论】:

    猜你喜欢
    • 2012-06-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-09-19
    相关资源
    最近更新 更多