要将TabLayout的指示器设置为固定宽度,可以通过自定义TabLayout的样式来实现。

首先,在res/values/styles.xml文件中定义一个新的样式,例如:

<style name="CustomTabLayout" parent="Widget.Design.TabLayout">
    <item name="tabIndicatorWidth">48dp</item>
</style>

这里设置了tabIndicatorWidth为48dp,即指示器的宽度为48dp。

然后,在布局文件中使用这个自定义的样式:

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

在TabLayout中添加了style属性,值为@style/CustomTabLayout。

最后,在代码中获取TabLayout实例,并设置Tab:

TabLayout tabLayout = findViewById(R.id.tabLayout);
tabLayout.addTab(tabLayout.newTab().setText("Tab 1"));
tabLayout.addTab(tabLayout.newTab().setText("Tab 2"));
tabLayout.addTab(tabLayout.newTab().setText("Tab 3"));

这样就可以将TabLayout的指示器设置为固定宽度了

TabLayout 修改指示器为固定宽度

原文地址: https://www.cveoy.top/t/topic/imr4 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录