我正在使用支持库更新我的应用程序中的字体。字体在运行时不会在AppCompatCheckBox上更新,但在布局预览中可以正常工作。我的应用程序中有两个styles.xml文件,我将AppBaseTheme应用于我的所有活动。
默认styles.xml
<style name="AppBaseTheme" parent="Theme.AppCompat.Light.NoActionBar">
<!--
Theme customizations available in newer API levels can go in
res/values-vXX/styles.xml, while customizations related to
backward-compatibility can go here.
-->
<item name="android:windowIsTranslucent">true</item>
<item name="android:windowActionBar">false</item>
<item name="colorPrimaryDark">@color/material_blue_700</item>
<item name="colorPrimary">@color/material_blue_500</item>
<!-- Android widgets styles overridden -->
<item name="android:checkboxStyle">@style/AppCheckBoxStyle</item>
<item name="checkboxStyle">@style/AppCheckBoxStyle</item>
<item name="android:radioButtonStyle">@style/AppRadioButtonStyle</item>
<item name="radioButtonStyle">@style/AppRadioButtonStyle</item>
</style>
<style name="AppCheckBoxStyle" parent="android:Widget.CompoundButton.CheckBox">
<item name="android:fontFamily">@font/allura_regular</item>
</style>
<style name="AppRadioButtonStyle" parent="android:Widget.CompoundButton.RadioButton">
<item name="android:fontFamily">@font/allura_regular</item>
</style>v21/styles.xml
<style name="AppBaseTheme" parent="Theme.AppCompat.Light.NoActionBar">
<!--
Theme customizations available in newer API levels can go in
res/values-vXX/styles.xml, while customizations related to
backward-compatibility can go here.
-->
<item name="android:windowIsTranslucent">true</item>
<item name="android:windowActionBar">false</item>
<item name="colorPrimaryDark">@color/material_blue_700</item>
<item name="colorPrimary">@color/material_blue_500</item>
<!-- Android widgets styles overridden -->
<item name="android:checkboxStyle">@style/AppCheckBoxStyle</item>
<item name="checkboxStyle">@style/AppCheckBoxStyle</item>
<item name="android:radioButtonStyle">@style/AppRadioButtonStyle</item>
<item name="radioButtonStyle">@style/AppRadioButtonStyle</item>
</style>
<style name="AppCheckBoxStyle" parent="Widget.AppCompat.CompoundButton.CheckBox">
<item name="android:fontFamily">@font/allura_regular</item>
</style>
<style name="AppRadioButtonStyle" parent="android:Widget.CompoundButton.RadioButton">
<item name="android:fontFamily">@font/allura_regular</item>
</style>发布于 2018-03-09 16:07:34
是的,android:fontFamily不能与AppCompatCheckbox一起工作。您可以像这样创建一个自定义的checkbox小部件,并将其用于您的目的。
public class MyCustomCheckBox extends AppCompatCheckBox {
public MyCustomCheckBox(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
init();
}
public MyCustomCheckBox(Context context, AttributeSet attrs) {
super(context, attrs);
init();
}
public MyCustomCheckBox(Context context) {
super(context);
}
private void init() {
Typeface tf = Typeface.createFromAsset(getContext().getAssets(),
"poppins_light.ttf");
setTypeface(tf);
}
}发布于 2019-02-21 10:29:27
来补充@Insane Developer的答案。如果使用新的可扩展标记语言字体,可以使用ResourcesCompat获取正确的字体。
class CustomFontCheckBox : CheckBox {
constructor(context: Context?) : super(context)
constructor(context: Context?, attrs: AttributeSet?) : super(context, attrs)
constructor(context: Context?, attrs: AttributeSet?, defStyleAttr: Int) : super(context, attrs, defStyleAttr)
init {
typeface = ResourcesCompat.getFont(context, R.font.my_font)
}
}发布于 2019-05-15 01:48:30
此处报告了此问题:https://issuetracker.google.com/issues/63250768
并在1.0.2版本的AppCompat库上进行了修复:https://mvnrepository.com/artifact/androidx.appcompat/appcompat/1.0.2
https://stackoverflow.com/questions/49189142
复制相似问题