【问题标题】:TabWidget - How to set position of indicator text?TabWidget - 如何设置指示器文本的位置?
【发布时间】:2010-01-31 01:32:05
【问题描述】:

我正在尝试在我的 Android 应用程序中使用 TabHost 和 TabWidget。这是我的布局 main.xml:

<TabHost
    android:id="@+id/mainTabHost"
    android:layout_width="fill_parent"
    android:layout_height="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="65px"/>
    <FrameLayout
        android:id="@android:id/tabcontent"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent">
        <LinearLayout
            android:id="@+id/contentTags"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:orientation="vertical">
        </LinearLayout>
        <LinearLayout
            android:id="@+id/contentPreferences"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:orientation="vertical">
        </LinearLayout>
    </FrameLayout>
</TabHost>

还有我的代码:

final TabHost mainTabHost = (TabHost) this.findViewById(R.id.mainTabHost);
mainTabHost.setup();
final TabHost.TabSpec tabSpecTags = mainTabHost.newTabSpec("tabTags");
tabSpecTags.setIndicator(this.getString(R.string.tab_name_tags));
tabSpecTags.setContent(R.id.contentTags);
mainTabHost.addTab(tabSpecTags);
final TabHost.TabSpec tabSpecPreferences = mainTabHost.newTabSpec("tabPreferences");
tabSpecPreferences.setIndicator(this.getString(R.string.tab_name_preferences));
tabSpecPreferences.setContent(R.id.contentPreferences);
mainTabHost.addTab(tabSpecPreferences);

问题是我不希望我的标签太高(65 像素)。但是,如果我将 TabWidget 的 layout_height 设置为例如30px,我根本看不到选项卡上的选项卡标签(指示器)。

TabWidget 似乎有一个“最低要求”高度(65px?),并且指示器位于这个 65px 的底部?

有没有办法调整指标的位置?

谢谢!

【问题讨论】:

  • 嗨,Edwin,您能否与我分享您的代码,了解如何使标签长度变小以及如何使文本按您的意愿显示。我遇到了同样的问题,因为当我降低标签的高度时,我失去了文本显示。

标签: android tabwidget


【解决方案1】:

但是,如果我将 layout_height 设置为 TabWidget 例如30px,我不能 见标签标签(指标) 完全没有标签。

请注意,这样做的技术在未来的 Android 版本中不一定可靠,如果它们改变了 TabHost 的构造方式。

有没有办法调整 指标的定位?

从 API 级别 4(又名 Android 1.6)开始,TabHost.TabSpec 通过 setIndicator() 接受 View 作为指示符。我没有尝试过,但它可以让您更好地控制单个选项卡指示器的布局。

【讨论】:

  • 谢谢!我试过了,是的,我可以使用带有 setIndicator 的 TextView。所以剩下的就是配置TextView。缺点是它基本上是从头开始的(默认情况下只是没有装饰的文本)。
  • 好吧,您总是可以为您的指标创建一个布局 XML 文件并对其进行扩展,将其用于setIndicator() 调用。
【解决方案2】:

我明白了... 当你 addTab 时,通常我们像这样使用 setIndicator: QTabHost.addTab(QTabHost.newTabSpec("tab_test2").setIndicator("TAB 2").bla bla....

你可以用TextView替换“TAB 2”,变成了这样:

tview=new TextView(this); tview.setText("这里的标题"); QTabHost.addTab(QTabHost.newTabSpec("tab_test2").setIndicator(tview).bla bla....

你只需要修改文本视图。 谢谢...^^

【讨论】:

    【解决方案3】:
    int iH = getTabWidget().getLayoutParams().height;
    
    for (int i = 0; i < tabHost.getTabWidget().getChildCount(); i++) {
                tabHost.getTabWidget().getChildAt(i).getLayoutParams().height = iH;
            }
    

    【讨论】:

    • 这对我很有效。我在 TabActivity 的 onCreate() 方法中使用此代码,我的布局文件有:
    【解决方案4】:
    for (int i = 0; i < 4; i++) {
        tabhost.getTabWidget().getChildAt(i).getLayoutParams().height = 50;
    }
    

    通过使用这个我们可以很容易地做到这一点

    【讨论】:

    • 此帖无效。您不能通过tabhost.getTabWidget().getChildAt(i).getLayoutParams().height = 50 设置标签的高度
    猜你喜欢
    • 2023-03-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-04-08
    • 1970-01-01
    相关资源
    最近更新 更多