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

时间:2010-01-31 01:32:06

标签: android tabwidget

我正在尝试在我的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的底部?

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

谢谢!

4 个答案:

答案 0 :(得分:4)

  

但是,如果我设置了layout_height   TabWidget例如30px,我不能   看到标签上的标签(指示灯)   完全没有标签。

请注意,如果他们更改了TabHost的构建方式,那么在未来的Android版本中执行此操作的技术不一定可靠。

  

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

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

答案 1 :(得分:2)

我知道...... 当你添加addTab时,我们通常会像这样使用setIndicator: QTabHost.addTab(QTabHost.newTabSpec(“tab_test2”)。setIndicator(“TAB 2”)。bla bla ....

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

tview = new TextView(this); tview.setText(“Title here”); QTabHost.addTab(QTabHost.newTabSpec(“tab_test2”)。setIndicator(tview).bla bla ....

所有你需要的只是修改textview。 谢谢...... ^^

答案 2 :(得分:1)

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

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

答案 3 :(得分:0)

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

使用这个我们可以很容易地做到

相关问题