如何在自定义字体系列中获得粗体样式?

问题描述 投票:0回答:2

我有以下自定义字体系列代码,并且我希望该字体具有字体粗细

abc.xml

<?xml version="1.0" encoding="utf-8"?>
<font-family
    xmlns:android="http://schemas.android.com/apk/res/android">

    <font
        android:font="@font/abc_regular"
        android:fontStyle="normal"
        android:fontWeight="400" />

    <font
        android:font="@font/abc_bold"
        android:fontStyle="normal"
        android:fontWeight="700" />
</font-family>

BaseButton.kt

open class BaseButton : AppCompatButton {

    @JvmOverloads
    constructor(
        context: Context,
        attrs: AttributeSet? = null,
        defStyleAttr: Int = 0)
            : super(context, attrs, defStyleAttr) {
        setFontFamily()
    }

    private fun setFontFamily() {
        val typeface = ResourcesCompat.getFont(context, R.font.abc)
        this.typeface = typeface
    }
}

screen_layout.xml

<com.aaa.bbb.ccc.BaseButton
    android:layout_width="100dp"
    android:layout_height="wrap_content"
    android:text="Test"
    android:textStyle="bold"
    android:textFontWeight="700" />

该按钮不显示粗体文本。怎么了?

android
2个回答
0
投票

因为在父级构造函数中设置了粗体样式之后,您才设置字体。试试这个


0
投票

我希望这对您有用。

© www.soinside.com 2019 - 2024. All rights reserved.