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

10

我在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"/>

这是Style主题 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属性可以设置选项卡中小指示器(活动选项卡)的高度。但是我们如何设置选项卡本身的高度?


1
只需将“height”替换为“android:layout_height =”your_height"”。 - H Raval
@HRaval java.lang.RuntimeException: 无法启动活动 ComponentInfo{MainActivity}: android.view.InflateException: 二进制 XML 文件行 #25: 二进制 XML 文件行 #25: 您必须提供 layout_height 属性。 - Suisse
3个回答

10

只需将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"/>

6

设置一个以dps为单位的layout_height,而不是wrap_content。这在不同的显示尺寸上可能会有所不同,但如果你想要动态设置高度,这样做会更好。

getApplication.getResources().getDisplayMetrics()

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


2
我使用以下代码来设置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);

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