【问题标题】:How to change the font family of SwitchCompat?如何更改 SwitchCompat 的字体系列?
【发布时间】:2018-04-16 06:00:10
【问题描述】:

我注意到 SwitchCompat 的 font 似乎并没有随着我在 fontFamily 字段中的设置而改变。我还尝试过将 styles 与自定义 fontFamily(适用于 TextViews)甚至 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>

还是不行

【问题讨论】:

    标签: android xml switchcompat


    【解决方案1】:

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

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

    【讨论】:

      【解决方案2】:

      试试

      parent="Base.TextAppearance.AppCompat.Body1"
      

      替换成这个

      parent="TextAppearance.AppCompat.Widget.Switch"
      

      【讨论】:

      • 不幸的是,它没有用,而是隐藏了拇指和它的滑块
      【解决方案3】:

      试试下面

      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 的自定义样式不起作用。
      • 我使用 com.android.support:appcompat-v7:27.1.1 也看到了这个问题。我正在应用一种包含字体系列以及使文本全部大写的样式。我看到样式的大写部分已正确应用于 SwitchCompat,但不是字体。
      【解决方案4】:

      唯一对我有用的是setSwitchTypeface不是 setTypeface:

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

      【讨论】:

        【解决方案5】:

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

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2021-06-07
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2023-03-20
          相关资源
          最近更新 更多