如何在 Android TabLayout 中设置选项卡的高度?

How can I set the height of tabs in Android a TabLayout?

我在 Android 中有此 TabLayout,并希望使标签比默认值 (48dp) 高一点

    <android.support.design.widget.TabLayout
            android:id="@+id/contentTabs"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            style="@style/Theme.Zhaw.TabLayout"
            app:tabMode="fixed"
            app:tabGravity="fill"/>

样式如下Theme.Zhaw.TabLayout:

<style name="Theme.Zhaw.TabLayout" parent="Widget.Design.TabLayout">
    <item name="tabIndicatorColor">@color/text_white</item>
    <item name="tabIndicatorHeight">4dp</item>
    <item name="tabPaddingStart">6dp</item>
    <item name="tabPaddingEnd">6dp</item>
    <item name="tabBackground">@color/colorPrimary</item>
    <item name="tabTextAppearance">@style/Theme.Zhaw.TabLayoutText</item>
    <item name="tabSelectedTextColor">@color/text_white</item>
</style>

tabIndicatorHeight 可以设置选项卡中小指示器(活动选项卡)的高度。但是我们如何设置选项卡本身的高度?

在 dps 中设置 layout_height 而不是 wrap_content 这可能会因不同的显示尺寸而有所不同,但如果您想动态设置高度

getApplication.getResources().getDisplayMetrics()

获取您当前的身高并根据该身高计算身高

只需将 layout_height 从 wrap_content 更改为您想要的

  <android.support.design.widget.TabLayout
            android:id="@+id/contentTabs"
            android:layout_width="match_parent"
            android:layout_height="Your Value"
            style="@style/Theme.Zhaw.TabLayout"
            app:tabMode="fixed"
            app:tabGravity="fill"/>

我使用下面的代码来设置 TabLayout 的高度。希望对你有帮助:

//Get tablayout
TabLayout tabLayout = (TabLayout) findViewById(R.id.tab_layout);
//Get the layout params that will allow you to resize the tablayout
ViewGroup.LayoutParams params = tabLayout.getLayoutParams();
//Change the height in 'Pixels'
params.height = 100;
tabLayout.setLayoutParams(params);