问题
i have been searching how to change the indicator in Tablayout to be circular, like this
but i don't know how to do so, any help!
回答1:
From the source code, the tab indicator is defined as:
<style name="Base.Widget.Design.TabLayout" parent="android:Widget">
<item name="tabMaxWidth">@dimen/design_tab_max_width</item>
<item name="tabIndicatorColor">?attr/colorAccent</item>
<item name="tabIndicatorHeight">2dp</item>
<item name="tabPaddingStart">12dp</item>
<item name="tabPaddingEnd">12dp</item>
<item name="tabBackground">?attr/selectableItemBackground</item>
<item name="tabTextAppearance">@style/TextAppearance.Design.Tab</item>
<item name="tabSelectedTextColor">?android:textColorPrimary</item>
</style>
and the tabIndicatorColor attribute is defined as:
<declare-styleable name="TabLayout">
<attr name="tabIndicatorColor" format="color"/>
<attr name="tabIndicatorHeight" format="dimension"/>
so I believe you can't change it to a drawable (shape), you can only change its color.
An alternative is to define a custom view for the tabs and handle the indicator state yourself
回答2:
1) Create layouts for your tab states(Ex: tab_selected.xml and tab_unselected.xml)
2) Set a custom view for your tabs using your layouts:
//get your tab item
TabLayout.Tab tabItem = tabLayout.getTabAt(i);
//inflate your layout to a view and set it as the tab's custom view
tabItem.setCustomView(customView);
3) Then, using setOnTabSelectedListener, keep listening to which tab is selected and update its custom view accordingly(using setCustomView)
回答3:
I think you should use custom view for tabs and change it with your TabLayout.OnTabSelectedListener
来源:https://stackoverflow.com/questions/36060196/tablayout-indicator-customization