[英]How to get bold style in custom font family?
我有以下自定義字體系列代碼,我想為該字體設置字體粗細
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>
基本按鈕.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" />
該按鈕不顯示粗體文本。 怎么了?
因為您是在父構造函數中設置粗體樣式后設置字體的。 試試這個
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)
}
我希望這對你有用。
在布局文件中添加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.