android.support.design.widget.TabLayout 的自定义字体
Custom font for android.support.design.widget.TabLayout
如何为属于 android.support.design.widget 包的 Tablayout class 使用自定义字体?我用它来实现快速 Return 查看功能。
试试这个 CustomTabLayout
public class CustomTabLayout extends TabLayout {
public CustomTabLayout(Context context) {
super(context);
}
public CustomTabLayout(Context context, AttributeSet attrs) {
super(context, attrs);
}
public CustomTabLayout(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
}
@Override
public void setTabsFromPagerAdapter(@NonNull PagerAdapter adapter) {
Typeface typeface = Typeface.createFromAsset(getContext().getAssets(), "fonts/Roboto-Medium.ttf");
this.removeAllTabs();
ViewGroup slidingTabStrip = (ViewGroup) getChildAt(0);
for (int i = 0, count = adapter.getCount(); i < count; i++) {
Tab tab = this.newTab();
this.addTab(tab.setText(adapter.getPageTitle(i)));
AppCompatTextView view = (AppCompatTextView) ((ViewGroup)slidingTabStrip.getChildAt(i)).getChildAt(1);
view.setTypeface(typeface, Typeface.NORMAL);
}
}
}
从 23.2.0 开始,setTabsFromPagerAdapter 已被弃用,但是使用 Andreyua 答案的修改版本,您可以改用 setupWithViewPager。
@Override
public void setupWithViewPager(ViewPager viewPager)
{
super.setupWithViewPager(viewPager);
if (mTypeface != null)
{
this.removeAllTabs();
ViewGroup slidingTabStrip = (ViewGroup) getChildAt(0);
PagerAdapter adapter = viewPager.getAdapter();
for (int i = 0, count = adapter.getCount(); i < count; i++)
{
Tab tab = this.newTab();
this.addTab(tab.setText(adapter.getPageTitle(i)));
AppCompatTextView view = (AppCompatTextView) ((ViewGroup) slidingTabStrip.getChildAt(i)).getChildAt(1);
view.setTypeface(mTypeface, Typeface.NORMAL);
}
}
}
所有功劳都归功于 Andreyua 他们的原始代码片段稍作修改。
不幸的是,我没有足够的声誉来发表评论,否则我会直接回复:)
使用 android 支持库 26.2.0 您可以在样式中指定字体
<style name="TabLayout" parent="Widget.Design.TabLayout">
<item name="tabTextAppearance">@style/TabText</item>
<item name="tabSelectedTextColor">@color/white</item>
<item name="tabIndicatorColor">@color/white</item>
</style>
<style name="TabText" parent="TextAppearance.Design.Tab">
<item name="android:textSize">14sp</item>
<item name="android:textColor">@color/lite</item>
<!--Here below-->
<item name="android:fontFamily">@font/gotham_medium</item>
</style>
• Kotlin Version
通过 Extension Function
要将自定义字体设置为 TabLayout
,请尝试在 kotlin 文件中添加以下代码片段作为扩展函数(例如,我创建了一个名为 Extentions.tk
的文件):
fun TabLayout.applyFont(typeface: Typeface) {
val viewGroup = getChildAt(0) as ViewGroup
val tabsCount = viewGroup.childCount
for (j in 0 until tabsCount) {
val viewGroupChildAt = viewGroup.getChildAt(j) as ViewGroup
val tabChildCount = viewGroupChildAt.childCount
for (i in 0 until tabChildCount) {
val tabViewChild = viewGroupChildAt.getChildAt(i)
if (tabViewChild is TextView) {
tabViewChild.typeface = typeface
}
}
}
}
.
• Usage
现在您可以简单地使用它了:
val typeface = Typeface.createFromAsset(context.assets, "fonts/Roboto.ttf")
tabLayout.applyFont(typeface)
如何为属于 android.support.design.widget 包的 Tablayout class 使用自定义字体?我用它来实现快速 Return 查看功能。
试试这个 CustomTabLayout
public class CustomTabLayout extends TabLayout {
public CustomTabLayout(Context context) {
super(context);
}
public CustomTabLayout(Context context, AttributeSet attrs) {
super(context, attrs);
}
public CustomTabLayout(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
}
@Override
public void setTabsFromPagerAdapter(@NonNull PagerAdapter adapter) {
Typeface typeface = Typeface.createFromAsset(getContext().getAssets(), "fonts/Roboto-Medium.ttf");
this.removeAllTabs();
ViewGroup slidingTabStrip = (ViewGroup) getChildAt(0);
for (int i = 0, count = adapter.getCount(); i < count; i++) {
Tab tab = this.newTab();
this.addTab(tab.setText(adapter.getPageTitle(i)));
AppCompatTextView view = (AppCompatTextView) ((ViewGroup)slidingTabStrip.getChildAt(i)).getChildAt(1);
view.setTypeface(typeface, Typeface.NORMAL);
}
}
}
从 23.2.0 开始,setTabsFromPagerAdapter 已被弃用,但是使用 Andreyua 答案的修改版本,您可以改用 setupWithViewPager。
@Override
public void setupWithViewPager(ViewPager viewPager)
{
super.setupWithViewPager(viewPager);
if (mTypeface != null)
{
this.removeAllTabs();
ViewGroup slidingTabStrip = (ViewGroup) getChildAt(0);
PagerAdapter adapter = viewPager.getAdapter();
for (int i = 0, count = adapter.getCount(); i < count; i++)
{
Tab tab = this.newTab();
this.addTab(tab.setText(adapter.getPageTitle(i)));
AppCompatTextView view = (AppCompatTextView) ((ViewGroup) slidingTabStrip.getChildAt(i)).getChildAt(1);
view.setTypeface(mTypeface, Typeface.NORMAL);
}
}
}
所有功劳都归功于 Andreyua 他们的原始代码片段稍作修改。
不幸的是,我没有足够的声誉来发表评论,否则我会直接回复:)
使用 android 支持库 26.2.0 您可以在样式中指定字体
<style name="TabLayout" parent="Widget.Design.TabLayout">
<item name="tabTextAppearance">@style/TabText</item>
<item name="tabSelectedTextColor">@color/white</item>
<item name="tabIndicatorColor">@color/white</item>
</style>
<style name="TabText" parent="TextAppearance.Design.Tab">
<item name="android:textSize">14sp</item>
<item name="android:textColor">@color/lite</item>
<!--Here below-->
<item name="android:fontFamily">@font/gotham_medium</item>
</style>
• Kotlin Version
通过 Extension Function
要将自定义字体设置为 TabLayout
,请尝试在 kotlin 文件中添加以下代码片段作为扩展函数(例如,我创建了一个名为 Extentions.tk
的文件):
fun TabLayout.applyFont(typeface: Typeface) {
val viewGroup = getChildAt(0) as ViewGroup
val tabsCount = viewGroup.childCount
for (j in 0 until tabsCount) {
val viewGroupChildAt = viewGroup.getChildAt(j) as ViewGroup
val tabChildCount = viewGroupChildAt.childCount
for (i in 0 until tabChildCount) {
val tabViewChild = viewGroupChildAt.getChildAt(i)
if (tabViewChild is TextView) {
tabViewChild.typeface = typeface
}
}
}
}
.
• Usage
现在您可以简单地使用它了:
val typeface = Typeface.createFromAsset(context.assets, "fonts/Roboto.ttf")
tabLayout.applyFont(typeface)