【问题标题】:How can I set the height of tabs in Android a TabLayout?如何在 Android 的 TabLayout 中设置选项卡的高度?
【发布时间】:2018-06-24 03:10:20
【问题描述】:

我在 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 可以设置标签中小指示器(活动标签)的高度。但是我们如何设置标签本身的高度呢?

【问题讨论】:

  • 只需将高度替换为 android:layout_height="your_height"
  • @HRaval java.lang.RuntimeException:无法启动活动 ComponentInfo{MainActivity}:android.view.InflateException:二进制 XML 文件第 25 行:二进制 XML 文件第 25 行:您必须提供 layout_height属性。

标签: android android-tablayout


【解决方案1】:

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

【讨论】:

    【解决方案2】:

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

    getApplication.getResources().getDisplayMetrics()
    

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

    【讨论】:

      【解决方案3】:

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

      【讨论】:

        猜你喜欢
        • 2017-03-11
        • 2019-07-10
        • 2013-03-23
        • 1970-01-01
        • 2018-04-03
        • 2016-11-11
        • 1970-01-01
        • 2016-02-11
        相关资源
        最近更新 更多