android tablayout选中的字体颜色及大小改变
2020-04-19 本文已影响0人
hao_developer
image.png
布局
<com.google.android.material.tabs.TabLayout
android:id="@+id/tabLay"
android:layout_width="match_parent"
android:layout_height="@dimen/dp_35"
android:layout_marginBottom="@dimen/dp_10"
app:tabIndicatorHeight="@dimen/dp_0"
app:tabMode="scrollable"
app:tabSelectedTextColor="@color/black"
app:tabTextAppearance="@style/TabLayoutTextSize"
app:tabTextColor="@color/silvery" />
style样式
<!--设置Tablayout字体大小-->
<style name="TabLayoutTextSize">
<item name="android:textSize">@dimen/sp_15</item>
<item name="android:textStyle">bold</item>
</style>
<!--设置Tablayout字体加粗-->
<style name="TabLayoutTextStyle">
<item name="android:textStyle">bold</item>
</style>
tablayout的监听事件处理
tabLay.addOnTabSelectedListener(object : TabLayout.OnTabSelectedListener{
override fun onTabReselected(tab: TabLayout.Tab?) {
}
override fun onTabUnselected(tab: TabLayout.Tab?) {
val title =
((tabLay.getChildAt(0) as LinearLayout).getChildAt(tab!!.position) as LinearLayout).getChildAt(
1
) as TextView
title.apply {
setTextAppearance(context, R.style.TabLayoutTextStyle)
setTypeface(Typeface.defaultFromStyle(Typeface.NORMAL))
}
}
override fun onTabSelected(tab: TabLayout.Tab?) {
tabIndex = tab?.position ?: 0
pageIndex = 1
val title =
((tabLay.getChildAt(0) as LinearLayout).getChildAt(tab!!.position) as LinearLayout).getChildAt(
1
) as TextView
title.apply {
setTextAppearance(context, R.style.TabLayoutTextStyle)
setTypeface(Typeface.defaultFromStyle(Typeface.BOLD))
}
}
})