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

标签 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 的底部?

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

谢谢!

最佳答案

However, if i set the layout_height of the TabWidget to e.g. 30px, i can't see the tab labels (indicator) on the tabs at all.

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

Is there a way to adjust the positioning of the indicator?

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

关于android - TabWidget - 如何设置指示器文本的位置?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2170200/

相关文章:

android - Android 的 Material Components 中新的 ExtendedFloatingActionButton 的收缩和扩展功能不起作用

javascript - 是否可以通过 WebView.addJavascriptInterface 从 JS 端接收对象?

python - PyQt TabWidget 不显示 ui 或接受 setCurrentWidget

android - Android 中不同大小的选项卡

android - 如何使用 Android Studio 开发 Google App Engine 后端

android - 通过 AsyncTask 更新双进度条

javascript - 在 android 旧版本(如果冻 bean 、kitkat android)中无法使用上一页功能,它仅适用于 Lollipop 版本

android - 无法在选项卡中获得不同的 View

android - 在选项卡小部件中使用 onPause、onRestart - Android