android design support TabLayout 更改标签文本字体 [英] Change the font of tab text in android design support TabLayout

查看:29
本文介绍了android design support TabLayout 更改标签文本字体的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试处理来自 android 设计库的新 TabLayout.

I'm trying to work on the new TabLayout from the android design library.

我想将标签文本更改为自定义字体.而且,我试图搜索一些与 TabLayout 相关的样式,但最终搜索到了 这个.

I want to change tab text to custom font. And,I tried to search some styling related to TabLayout,but ended up to this.

请指导我如何更改标签文本字体.

Please guide how can I change the tab text fonts.

推荐答案

像这样从 Java 代码或 XML 创建一个 TextView

Create a TextView from Java Code or XML like this

<?xml version="1.0" encoding="utf-8"?>
<TextView
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@android:id/text1"
    android:layout_width="match_parent"
    android:textSize="15sp"
    android:textColor="@color/tabs_default_color"
    android:gravity="center"
    android:layout_height="match_parent"
/>

请确保将 id 保持原样,因为如果您使用自定义文本视图,TabLayout 会检查此 ID

Make sure to keep the id as it is here because the TabLayout check for this ID if you use custom textview

然后从代码中扩展这个布局并在那个 textview 上设置自定义 Typeface 并将这个自定义视图添加到选项卡

Then from code inflate this layout and set the custom Typeface on that textview and add this custom view to the tab

for (int i = 0; i < tabLayout.getTabCount(); i++) {
     //noinspection ConstantConditions
     TextView tv = (TextView)LayoutInflater.from(this).inflate(R.layout.custom_tab,null)
     tv.setTypeface(Typeface);       
     tabLayout.getTabAt(i).setCustomView(tv);
}

这篇关于android design support TabLayout 更改标签文本字体的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆