如何更改SwitchCompat的字体族?

3
我注意到SwitchCompat的字体似乎并不随我在fontFamily字段中设置的内容而改变。我还尝试使用带有自定义fontFamily的样式(这在TextView上有效),甚至是switchTextAppearance字段。它确实适用于预览(我知道预览不是真正准确的),但当我尝试在我的测试设备上运行它时却没有效果。
以下是我的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"/>

以下是我的样式:

<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>

如您所见,我只想更改它的字体。
编辑

我已将样式更改为这样

<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>

尽管如此,仍然不起作用。
5个回答

1

XML设置无法正常工作,因此我使用以下代码:

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

0

请尝试以下操作

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文件

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

使用自定义字体实现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"
        />

在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>

自定义类将会工作(需要一些小的更改来使用新的字体家族xml,ResourceCompat.getFont()),我已经有了类似的东西,但这是我目前的最后选择...不幸的是,通过_app:theme_的自定义样式并不能起作用。 - Gama the Great
我也遇到了这个问题,使用com.android.support:appcompat-v7:27.1.1。我应用了一个包含字体系列以及将文本全部转换为大写的样式。我发现样式中的大写部分已经正确应用于SwitchCompat,但字体没有。 - vonWippersnap

0

对我有效的唯一方法是使用setSwitchTypeface,而不是setTypeface

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

0

尝试

parent="Base.TextAppearance.AppCompat.Body1"

替换成这个

parent="TextAppearance.AppCompat.Widget.Switch"

很遗憾,它没有起作用,反而隐藏了拇指和滑块。 - Gama the Great

0

我现在使用了android:textAppearance而不是android:switchTextAppearance,自定义字体现在可以正常工作了。此外,我的开关样式的父级为Widget.AppCompat.CompoundButton.Switch


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接