Last active
February 18, 2021 06:28
-
-
Save CodeK1988/a05e7dd1c9c6cc02962460b68f20bc99 to your computer and use it in GitHub Desktop.
TabLayout custom change tabTextSize
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
1.tabTextAppearance | |
<style name="YourTextAppearance" parent="TextAppearance.AppCompat.Button"> | |
<item name="android:textSize">20sp</item> | |
... | |
</style> | |
<com.google.android.material.tabs.TabLayout | |
android:id="@+id/tabLayout" | |
android:layout_width="match_parent" | |
android:layout_height="wrap_content" | |
android:layout_gravity="top" | |
android:layout_marginTop="115dp" | |
app:layout_constraintTop_toTopOf="parent" | |
app:tabTextAppearance="@style/YourTextAppearance" | |
app:tabSelectedTextColor="@android:color/white" | |
app:tabTextColor="@color/percent10OfWhite" /> | |
2.XML | |
tabLayout.addOnTabSelectedListener(object : TabLayout.OnTabSelectedListener { | |
override fun onTabReselected(tab: TabLayout.Tab?) { | |
} | |
override fun onTabUnselected(tab: TabLayout.Tab?) { | |
(tab?.customView?.findViewById(R.id.tvTabItem) as TextView).textSize = 18f | |
(tab?.customView?.findViewById(R.id.tvTabItem) as TextView).textColor =R.color.percent30OfWhite | |
} | |
override fun onTabSelected(tab: TabLayout.Tab?) { | |
(tab?.customView?.findViewById(R.id.tvTabItem) as TextView).textSize = 20f | |
(tab?.customView?.findViewById(R.id.tvTabItem) as TextView).textColor = Color.WHITE | |
} | |
}) | |
TabLayoutMediator(tabLayout, viewPager2, | |
TabLayoutMediator.TabConfigurationStrategy { tab, position -> | |
// tab.text = listBean.skill[position].title | |
val inflate = | |
layoutInflater.inflate(R.layout.tablayout_item, tabLayout, false) | |
tab.customView = inflate | |
inflate.findViewById<TextView>(R.id.tvTabItem).text = listBean.skill[position].title | |
}).attach() | |
tablayout_item.xml | |
<?xml version="1.0" encoding="utf-8"?> | |
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" | |
xmlns:tools="http://schemas.android.com/tools" | |
android:layout_width="match_parent" | |
android:layout_height="match_parent" | |
android:gravity="center" | |
android:orientation="vertical"> | |
<androidx.appcompat.widget.AppCompatTextView | |
android:id="@+id/tvTabItem" | |
android:layout_width="wrap_content" | |
android:layout_height="wrap_content" | |
tools:text="test" /> | |
</LinearLayout> | |
thanks | |
https://stackoverflow.com/questions/44244469/how-to-change-selected-tab-title-textsize-in-android | |
https://stackoverflow.com/questions/56271392/cannot-set-tabitem-text-and-icon-size-is-always-the-same-size | |
https://material.io/develop/android/components/tab-layout/ | |
https://github.com/material-components/material-components-android/blob/master/lib/java/com/google/android/material/tabs/res/values/styles.xml | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment