clicked:

normal:

resource:
<selector android:constantSize="true" xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="@drawable/tab_item_indicator_checked" android:state_selected="true"/>
<item android:drawable="@drawable/tab_item_indicator_normal"/>
</selector>
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<ImageView
android:id="@+id/imageView"
android:src="@drawable/tab_item_indicator_selector"
android:layout_gravity="bottom|center"
android:layout_width="40dp"
android:layout_height="8dp"/>
<TextView
android:id="@android:id/text1"
android:layout_width="wrap_content"
android:textAllCaps="false"
android:layout_gravity="center"
android:textColor="@color/color_999999"
android:layout_height="match_parent"/>
code:
/**
* created by intbird
* on 2020/1/18
*/
class HomeTabLayout : TabLayout {
constructor(context: Context?) : super(context!!)
constructor(context: Context?, attrs: AttributeSet?) : super(context!!, attrs)
constructor(context: Context?, attrs: AttributeSet?, defStyleAttr: Int) : super(context!!, attrs, defStyleAttr)
init {
setSelectedTabIndicator(null)
tabGravity = GRAVITY_FILL
setTabRippleColorResource(R.color.txt_orange_light)
setBackgroundColor(Color.TRANSPARENT)
setUnboundedRipple(false)
val padding = ScreenUtils.dp2px(context, 16)
setPadding(padding,0,0,padding)
addOnTabSelectedListener(object : OnTabSelectedListener {
override fun onTabSelected(tab: Tab?) {
val view = tab?.customView
val textView = view?.findViewById<TextView>(android.R.id.text1)
textView?.typeface = Typeface.DEFAULT_BOLD
textView?.textSize = 18f
textView?.setTextColor(ContextCompat.getColor(context, R.color.color_333333))
}
override fun onTabUnselected(tab: Tab?) {
val view = tab?.customView
val textView = view?.findViewById<TextView>(android.R.id.text1)
textView?.typeface = Typeface.DEFAULT
textView?.textSize = 14f
textView?.setTextColor(ContextCompat.getColor(context, R.color.color_999999))
}
override fun onTabReselected(tab: Tab?) {
}
})
}
// type 1
override fun newTab(): Tab {
return super.newTab()
}
override fun addTab(tab: Tab) {
super.addTab(tab)
}
override fun addTab(tab: Tab, position: Int) {
super.addTab(tab, position)
}
override fun addTab(tab: Tab, setSelected: Boolean) {
super.addTab(tab, setSelected)
}
override fun addTab(tab: Tab, position: Int, setSelected: Boolean) {
tab.customView = LayoutInflater.from(context).inflate(R.layout.view_tab_item_content, null)
if (!TextUtils.isEmpty(tab.text)) {
tab?.customView?.findViewById<TextView>(android.R.id.text1)?.text = tab.text
}
super.addTab(tab, position, setSelected)
}
// type 2
fun resetTabView(arrayTitle: ArrayList<String>?) {
for (i in 0 until tabCount) {
val tab = getTabAt(i)
tab?.customView = getTabView(arrayTitle, i)
val layoutParams = tab?.customView?.layoutParams as? LinearLayout.LayoutParams
layoutParams?.width = ViewGroup.LayoutParams.WRAP_CONTENT
tab?.customView?.layoutParams = layoutParams
}
}
fun resetTabViewInternal(arrayTitle: ArrayList<String>?) {
for (i in 0 until tabCount) {
val tab = getTabAt(i)
tab?.customView = getTabView(arrayTitle, i)
val layoutParams = tab?.customView?.layoutParams as? LinearLayout.LayoutParams
layoutParams?.width = ViewGroup.LayoutParams.WRAP_CONTENT
tab?.customView?.layoutParams = layoutParams
}
}
private fun getTabView(arrayTitle: ArrayList<String>?, position: Int): View? {
val v: View = LayoutInflater.from(context).inflate(R.layout.view_tab_item_content, null)
val tv = v.findViewById(android.R.id.text1) as? TextView
tv?.text = arrayTitle?.get(position) ?: ""
return v
}
}