1. 替换全局字体
需要引入依赖
// 替换全局字体使用
api 'com.android.support:support-v13:28.0.0'
在 res 目录下创建一个 font 目录,将用到的字体复制进去,然后再创建一个字体的xml。
替换字体01.jpg
xmlns:app="http://schemas.android.com/apk/res-auto">
android:font="@font/pop_ttf"
android:fontStyle="normal"
android:fontWeight="400"
app:font="@font/pop_ttf"
app:fontStyle="normal"
app:fontWeight="400" />
android:font="@font/pop_ttf"
android:fontStyle="italic"
android:fontWeight="400"
app:font="@font/pop_ttf"
app:fontStyle="italic"
app:fontWeight="400" />
在主题中引用
@font/pop_font
2. 在实际使用中,发现对 RadioButton 不起作用,所以自定义一个 RadioButton 单独对其进行处理;
添加属性;
继承 RadioButton;
class MyRadioButton @JvmOverloads constructor(
context: Context,
attrs: AttributeSet? = null,
defStyleAttr: Int = 0
) : RadioButton(context, attrs, defStyleAttr) {
private var textFont: Int = -1
init {
val typedArray = context.obtainStyledAttributes(attrs, R.styleable.MyRadioButton) //获取我们定义的属性
textFont = typedArray.getResourceId(R.styleable.MyRadioButton_textFont, -1)
initView()
typedArray.recycle()
}
private fun initView() {
if (-1 != textFont) {
paint.isAntiAlias = true
val font = ResourcesCompat.getFont(context, textFont)
paint.typeface = font
}
}
}
使用;
android:id="@+id/rb_mine"
style="@style/MainBtmTabRBtn"
android:text="我的"
app:textFont="@font/pop_font" />