简体   繁体   English

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

[英]How to get bold style in custom font family?

I have following custom font family code, and i want to have font weight for that font我有以下自定义字体系列代码,我想为该字体设置字体粗细

abc.xml 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基本按钮.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 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" />

The button does not show bolded text.该按钮不显示粗体文本。 What is wrong?怎么了?

Because you are setting typeface after the bold style is set up in the parent constructor.因为您是在父构造函数中设置粗体样式后设置字体的。 Try this one试试这个

    private fun setFontFamily() {
        val typeface = ResourcesCompat.getFont(context, R.font.sf_pro)
        val isBold = getTypeface().isBold

        this.setTypeface(typeface, if (isBold) Typeface.BOLD else Typeface.NORMAL)
    }

I hope this will work for you.我希望这对你有用。

Add fontFamily attribute in your layout file.在布局文件中添加fontFamily属性。

<com.aaa.bbb.ccc.BaseButton
    android:layout_width="100dp"
    android:layout_height="wrap_content"
    android:text="Test"
    android:fontFamily="@font/abc_bold" />

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何使用粗体创建自定义字体系列 - How to create custom font family with bold font 如何将粗体添加到自定义字体系列然后将其用于字符串? - How do I add bold to a custom font family then use it for strings? 自定义字体和粗体样式的文本 - Text with custom font and bold style 如何在 font_family.xml 中使用属性 android:fontStyle=&quot;bold&quot; 设置自定义字体 - How to set custom font in font_family.xml with attribute android:fontStyle=“bold” 如何知道通知中默认样式的字体系列? - How to know the font family of default style in notification? 如何在 java 中添加字体样式和粗体 - How to add font style and bold in java Flutter:未应用自定义字体系列 - Flutter: Custom font family do not get applied 如何通过样式将自定义字体样式/系列应用于整个应用程序中的警报对话框 - How to apply custom font style/ family to alert dialogs in entire app through styles Android将Roboto字体设置为粗体,斜体,常规,…(类似于自定义字体系列) - Android set Roboto font with bold, italic, regular,… (something like custom font family) 在文本视图中使用自定义字体设置粗体和斜体样式(以编程方式) - Set bold and italic style in a textview with custom font (Programatically)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM