web-dev-qa-db-ja.com

Android TabLayoutでタブの高さを設定するにはどうすればよいですか?

私はこのTabLayoutをAndroidに持っていて、タブをデフォルト(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は、タブ内の小さなインジケーター(アクティブなタブ)の高さを設定できます。しかし、タブ自体の高さをどのように設定できますか?

5
Suisse

wrap_contentではなくdpsでlayout_heightを設定します。これは表示サイズによって異なる場合がありますが、高さを動的に設定する場合は

getApplication.getResources().getDisplayMetrics()

現在の身長を取得し、それに応じて身長を計算します

5
Sajidh Zahir

layout_heightをwrap_contentからuが望むものに変更するだけです

  <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
ArashSM79

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);
3
Mahyar