首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >在安卓设计支持TabLayout中更改选项卡文本的字体

在安卓设计支持TabLayout中更改选项卡文本的字体
EN

Stack Overflow用户
提问于 2015-06-26 15:20:02
回答 19查看 96.2K关注 0票数 127

我正在尝试开发android设计库中的新TabLayout

我想将制表符文本更改为自定义字体。而且,我试图搜索一些与TabLayout相关的样式,但最终搜索到了this

请指导我如何更改制表符文本字体。

EN

回答 19

Stack Overflow用户

回答已采纳

发布于 2017-07-31 14:57:27

从Java Code或XML创建一个TextView,如下所示

代码语言:javascript
运行
复制
<?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

然后,从代码中膨胀此布局,在该文本视图上设置自定义Typeface,并将此自定义视图添加到选项卡

代码语言:javascript
运行
复制
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);
}
票数 39
EN

Stack Overflow用户

发布于 2015-06-26 15:28:11

如果您正在使用TabLayout,并且您想要更改字体,则必须在以前的解决方案中添加一个新的for循环,如下所示:

代码语言:javascript
运行
复制
private void changeTabsFont() {
    ViewGroup vg = (ViewGroup) tabLayout.getChildAt(0);
        int tabsCount = vg.getChildCount();
        for (int j = 0; j < tabsCount; j++) {
            ViewGroup vgTab = (ViewGroup) vg.getChildAt(j);
            int tabChildsCount = vgTab.getChildCount();
            for (int i = 0; i < tabChildsCount; i++) {
                View tabViewChild = vgTab.getChildAt(i);
                if (tabViewChild instanceof TextView) {
                    ((TextView) tabViewChild).setTypeface(Font.getInstance().getTypeFace(), Typeface.NORMAL);
                }
        }
    }
} 

请参考change font style in action bar tabs using sherlock

票数 170
EN

Stack Overflow用户

发布于 2017-08-22 19:51:08

创建您自己的自定义样式并将父样式用作parent="@android:style/TextAppearance.Widget.TabWidget"

并在选项卡布局中使用此样式作为app:tabTextAppearance="@style/tab_text"

示例: Style:

代码语言:javascript
运行
复制
<style name="tab_text" parent="@android:style/TextAppearance.Widget.TabWidget">
    <item name="android:fontFamily">@font/poppins_regular</item>
</style>

示例:选项卡布局组件:

代码语言:javascript
运行
复制
<android.support.design.widget.TabLayout
        android:id="@+id/tabLayout"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="?attr/colorPrimary"
        android:minHeight="?attr/actionBarSize"
        android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
        app:tabTextAppearance="@style/tab_text" />
票数 133
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/31067265

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档