简体   繁体   English

SwitchCompat fontfamily不会改变

[英]SwitchCompat fontfamily won't change

I'm using Android Studio and have a SwitchCompat widget in my main activity. 我正在使用Android Studio并在我的主要活动中有一个SwitchCompat小部件。 The default fontfamily it had was sans-serif-medium and I changed it to quicksand_light. 它的默认fontfamily是sans-serif-medium,我把它改成了quicksand_light。 I also have some TextViews with each of their fontfamily's set to quicksand_light. 我还有一些TextViews,每个fontfamily都设置为quicksand_light。 On the design tab of the xml file for my activity it shows the SwitchCompat having the quicksand_light fontfamily just as the TextViews, but when I run it on my phone or on an emulator the SwitchCompat's fontfamily is sans-serif-medium. 在我的活动的xml文件的设计选项卡上,它显示了具有quicksand_light fontfamily的SwitchCompat,就像TextViews一样,但是当我在手机或模拟器上运行它时,SwitchCompat的fontfamily是sans-serif-medium。 Is there something extra I need to do to change the fontfamily or is this a bug or is this just me? 有什么额外的东西我需要做更改fontfamily或这是一个错误或这只是我?

I haven't dived deep into why it's not working correctly when defining the fontFamily attribute in xml, but IT WORKS if you set the typeface programmatically . 我还没有深深地潜入为什么定义时,它不能正常工作fontFamily的XML属性,但如果你设置的字体编程 它的作品

Here's an example using data-binding. 这是使用数据绑定的示例。

Add the following data-binding adapter: 添加以下数据绑定适配器:

@BindingAdapter("labelTypeface")
fun setLabelTypeface(view: SwitchCompat, @FontRes id: Int) {
    view.typeface = ResourcesCompat.getFont(view.context, id)
}

and use it in your layout: 并在您的布局中使用它:

<android.support.v7.widget.SwitchCompat
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    app:labelTypeface="@{R.font.stratum}"
    ... />

我能够在开关的拇指(而不是其标签)上设置字体系列的唯一方法是:

    my_switch.setSwitchTypeface(ResourcesCompat.getFont(context, R.font.my_font))

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM