简体   繁体   English

如何更改 SwitchCompat 的字体系列?

[英]How to change the font family of SwitchCompat?

I've noticed that the SwitchCompat's font does not seem to change with what I've set in the fontFamily field.我注意到 SwitchCompat 的字体似乎没有随我在fontFamily字段中设置的内容而改变。 I've also tried using styles with custom fontFamily ( which works on TextViews ) and even the switchTextAppearance field.我已经使用样式定制fontFamily中(这在TextViews作品),甚至switchTextAppearance场也试过。 It does apply on the preview ( I know the preview is not really accurate ) but not when I tried running it on my test device.它确实适用于预览(我知道预览不是很准确)但不适用于我尝试在我的测试设备上运行它。

Here's my SwitchCompat:这是我的 SwitchCompat:

<android.support.v7.widget.SwitchCompat
            style="@style/MyStyle.Body"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:fontFamily="@font/my_font_family"
            android:text="Enable"
            app:switchTextAppearance="@style/MyStyle.Body"/>

and here's my style:这是我的风格:

<style name="MyStyle.Body" parent="Base.TextAppearance.AppCompat.Body1">
    <item name="android:textSize">14sp</item>
    <item name="android:fontFamily">@font/my_font_family</item>
    <item name="android:textColor">@color/color_primary_text</item>
</style>

As you can see, I only really want to change it's font如你所见,我只想改变它的字体


EDIT编辑

I've changed my style to this我已经改变了我的风格

<style name="MyStyle.Switch" parent="Widget.AppCompat.CompoundButton.Switch">
    <item name="android:textSize">14sp</item>
    <item name="android:textColor">@color/color_primary_text</item>
    <item name="android:fontFamily">@font/my_font_family</item>
    <item name="fontFamily">@font/my_font_family</item>
</style>

still doesn't work though不过还是不行

XML 设置不起作用,因此我使用以下代码:

someSwitch.setTypeface(ResourcesCompat.getFont(context, R.font.roboto));

Try试试

parent="Base.TextAppearance.AppCompat.Body1"

replace into this换成这个

parent="TextAppearance.AppCompat.Widget.Switch"

Try below试试下面

public class CustomSwitchCompact extends SwitchCompat {

    public CustomSwitchCompact(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        init();
    }

    public CustomSwitchCompact(Context context, AttributeSet attrs) {
        super(context, attrs);
        init();
    }

    public CustomSwitchCompact(Context context) {
        super(context);
        init();
    }

    private void init() {
        if (!isInEditMode()) {
            Typeface myFonts = Typeface.createFromAsset(getContext().getAssets(),
                    "fonts/Roboto_Bold.ttf");
            setTypeface(myFonts);
        }
    }
}

XML file XML文件

<com.test.CustomSwitchCompact
        android:id="@+id/switch_compat"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:checked="false"
        android:padding="20dp"
        android:text="SwitchCompat"
        android:textOff="OFF"
        android:textOn="ON"
        app:showText="true" />

Another way to achieve SwitchCompact with custom font使用自定义字体实现 SwitchCompact 的另一种方法

  <android.support.v7.widget.SwitchCompat
        xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/adamSwitch"
        android:textColor="@color/top_color"
        android:textAppearance="@color/top_color"
        android:gravity="center"
        app:showText="true"
        android:fontFamily="@font/my_font_family"
        app:theme="@style/Custom.Widget.SwitchCompat"
        app:switchPadding="5dp"
        />

in style.xml在 style.xml 中

<style name="Custom.Widget.SwitchCompat" parent="Widget.AppCompat.CompoundButton.Switch" >
            <item name="android:textColorPrimary">@color/blue</item>  
            <item name="android:textSize">14sp</item>
            <item name="android:fontFamily">@font/my_font_family</item>
             <item name="android:textColor">@color/color_primary_text</item>
      </style>

唯一对我setSwitchTypefacesetSwitchTypeface而不是setTypeface

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

I've used android:textAppearance instead of android:switchTextAppearance and custom font works now.我已经使用android:textAppearance而不是android:switchTextAppearance并且自定义字体现在可以使用了。 Also my switch style have Widget.AppCompat.CompoundButton.Switch as parent另外我的开关样式有Widget.AppCompat.CompoundButton.Switch作为父

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

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