SwitchCompat fontfamily won't change

六月ゝ 毕业季﹏ 提交于 2020-01-03 08:45:24

问题


I'm using Android Studio and have a SwitchCompat widget in my main activity. The default fontfamily it had was sans-serif-medium and I changed it to quicksand_light. I also have some TextViews with each of their fontfamily's set to 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. Is there something extra I need to do to change the fontfamily or is this a bug or is this just me?


回答1:


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.

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}"
    ... />



回答2:


The only way I've been able to set the font family on the switch's thumb (not its label) is with:

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


来源:https://stackoverflow.com/questions/48140043/switchcompat-fontfamily-wont-change

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!