美文网首页
android tablayout选中的字体颜色及大小改变

android tablayout选中的字体颜色及大小改变

作者: hao_developer | 来源:发表于2020-04-19 10:45 被阅读0次
    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))
                    }
                }
            })
    

    相关文章

      网友评论

          本文标题:android tablayout选中的字体颜色及大小改变

          本文链接:https://www.haomeiwen.com/subject/iamybhtx.html