{"title":"TabLayout 修改指示器为固定宽度","content":"要将TabLayout的指示器设置为固定宽度,可以通过自定义TabLayout的样式来实现。\n\n首先,在res/values/styles.xml文件中定义一个新的样式,例如:\n\nxml\n<style name="CustomTabLayout" parent="Widget.Design.TabLayout">\n <item name="tabIndicatorWidth">48dp</item>\n</style>\n\n\n这里设置了tabIndicatorWidth为48dp,即指示器的宽度为48dp。\n\n然后,在布局文件中使用这个自定义的样式:\n\nxml\n<android.support.design.widget.TabLayout\n android:id="@+id/tabLayout"\n android:layout_width="match_parent"\n android:layout_height="wrap_content"\n style="@style/CustomTabLayout"\n app:tabMode="fixed"\n app:tabGravity="fill" />\n\n\n在TabLayout中添加了style属性,值为@style/CustomTabLayout。\n\n最后,在代码中获取TabLayout实例,并设置Tab:\n\njava\nTabLayout tabLayout = findViewById(R.id.tabLayout);\ntabLayout.addTab(tabLayout.newTab().setText("Tab 1"));\ntabLayout.addTab(tabLayout.newTab().setText("Tab 2"));\ntabLayout.addTab(tabLayout.newTab().setText("Tab 3"));\n\n\n这样就可以将TabLayout的指示器设置为固定宽度了。"}

Android TabLayout 指示器固定宽度设置教程 - 自定义样式实现

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

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