TabWidget - 如何设置指示器文本的位置?

4

我将尝试在我的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);

问题是我不想让我的选项卡那么高(65px)。但是,如果我将TabWidget的layout_height设置为30px,我根本看不到选项卡上的标签(指示器)。
似乎TabWidget有一个“最小要求”高度(65px?),而指示器位于这65px的底部?
有没有办法调整指示器的位置?
谢谢!

嗨,Edwin,你能否把你的代码分享给我?我想知道怎样让标签页变小,并使文本显示符合要求。我也遇到了同样的问题,每次我缩小标签页高度时都会导致文本消失。 - skvyas
4个回答

4
然而,如果我将TabWidget的layout_height设置为例如30px,则根本看不到选项卡上的标签(指示器)。 请注意,如果他们改变了TabHost的构造方式,这样做的技术在未来的Android版本中不一定可靠。 有没有办法调整指示器的位置? 从API Level 4(即Android 1.6)开始,通过setIndicator()方法,TabHost.TabSpec接受View作为指示器。我还没有尝试过,但它可能使您更好地控制单个选项卡指示器的布局。

1
谢谢!我试过了,是的,我可以使用带有setIndicator的TextView。所以现在只剩下配置TextView了。不足之处在于它基本上是从头开始(默认情况下只是没有装饰的文本)。 - Edwin Lee
你可以为你的指示器创建一个布局XML文件并将其填充,然后在setIndicator()调用中使用它。 - CommonsWare

2

我明白了...... 在添加选项卡时,通常我们会使用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....
你只需要修改TextView即可。 谢谢... ^^


1
int iH = getTabWidget().getLayoutParams().height;

for (int i = 0; i < tabHost.getTabWidget().getChildCount(); i++) {
            tabHost.getTabWidget().getChildAt(i).getLayoutParams().height = iH;
        }

这对我非常有效。我在TabActivity的onCreate()方法中使用此代码,我的布局文件具有以下内容: <TabWidget android:id="@android:id/tabs" android:layout_width="fill_parent" android:layout_height="@dimen/tab_widget_height" /> - greg7gkb

0
for (int i = 0; i < 4; i++) {
    tabhost.getTabWidget().getChildAt(i).getLayoutParams().height = 50;
}

通过使用这个,我们可以非常容易地完成。


1
这篇文章不起作用。您不能通过 tabhost.getTabWidget().getChildAt(i).getLayoutParams().height = 50 来设置选项卡的高度。 - user553834

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接