【问题标题】:How to set custom font for TabLayout in Android?如何在 Android 中为 TabLayout 设置自定义字体?
【发布时间】:2016-06-17 00:42:04
【问题描述】:

我想在我的应用程序中使用TabLayoutViewPager,但我想为TabLayout 设置自定义字体,但我不能这样做!
我用这个代码:

Typeface droidSerifMonoTF = Typeface.createFromAsset(getAssets(), "fonts/DroidSerif.ttf");

for (int i = 0; i < mSectionsPagerAdapter.getCount(); i++) {
            TextView t = new TextView(this);
            t.setText(mSectionsPagerAdapter.getPageTitle(i) );
            t.setTypeface(droidSansMonoTF);

            actionBar.addTab(actionBar.newTab()
                    .setCustomView(t)
                    .setTabListener(this));

        }

来自此链接:Link 但不要为我工作!
我该怎么办?

【问题讨论】:

标签: android


【解决方案1】:

您可以通过扩展 TabLayout 类来做到这一点。重写 onLayout 方法如下:

@Override
protected void onLayout(boolean changed, int left, int top, int right, int bottom){
    super.onLayout(changed, left, top, right, bottom);

    final ViewGroup tabStrip = (ViewGroup)getChildAt(0);
    final int tabCount = tabStrip.getChildCount();
    ViewGroup tabView;
    int tabChildCount;
    View tabViewChild;

    for(int i=0; i<tabCount; i++){
        tabView = (ViewGroup)tabStrip.getChildAt(i);
        tabChildCount = tabView.getChildCount();
        for(int j=0; j<tabChildCount; j++){
            tabViewChild = tabView.getChildAt(j);
            if(tabViewChild instanceof AppCompatTextView){
                if(fontFace == null){
                    fontFace = Typeface.createFromAsset(context.getAssets(), context.getString(R.string.IranSans));
                }
                ((TextView) tabViewChild).setTypeface(fontFace, Typeface.BOLD);
            }
        }
    }
}

必须覆盖 onLayout 方法,因为当您使用 setupWithViewPager 方法将 TabLayout 与 ViewPager 绑定时,您必须在之后使用 setText 方法或在 PagerAdapter 中设置选项卡文本以及发生这种情况时, onLayout 方法在父 ViewGroup (TabLayout) 上被调用,这就是设置字体的地方。(更改 TextView 文本会导致调用其父级的 onLayout 方法 - tabView 有两个子级,一个是 ImageView 另一个是 TextView)

另一种解决方案:

首先,这几行代码:

if(fontFace == null){
    fontFace = Typeface.createFromAsset(context.getAssets(), context.getString(R.string.IranSans));
}

在上面的解决方案中,应该写在两个循环之外。

API >= 16 的更好解决方案是使用 android:fontFamily

创建一个 Android 资源目录 命名字体并将所需的字体复制到该目录。

然后使用这些样式:

<style name="tabLayoutTitles">
    <item name="android:textColor">@color/white</item>
    <item name="android:textSize">@dimen/appFirstFontSize</item>
    <item name="android:fontFamily">@font/vazir_bold</item>
</style>

<style name="defaultTabLayout">
    <item name="android:layout_width">match_parent</item>
    <item name="android:layout_height">@dimen/defaultTabLayoutHeight</item>
    <item name="android:gravity">right</item>
    <item name="tabTextAppearance">@style/tabLayoutTitles</item>
    <item name="tabSelectedTextColor">@color/white</item>
    <item name="tabIndicatorColor">@color/white</item>
    <item name="tabIndicatorHeight">@dimen/accomTabIndicatorHeight</item>
    <item name="tabMode">fixed</item>
    <item name="tabGravity">fill</item>
    <item name="tabBackground">@drawable/rectangle_white_ripple</item>
    <item name="android:background">@color/colorPrimary</item>
</style>

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-12-15
    • 2017-01-20
    • 2016-04-02
    • 1970-01-01
    • 2022-12-23
    • 1970-01-01
    • 2014-07-01
    相关资源
    最近更新 更多