Android 为什么“fontFamily”可以在“TextView”上工作,而不能在“Switch”上工作?

Android 为什么“fontFamily”可以在“TextView”上工作,而不能在“Switch”上工作?,android,android-layout,Android,Android Layout,我正在尝试使用。我有两个视图,一个是TextView: <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Blaaah" android:textSize="16sp" app:fontFamily="@font/my_custom_font" /> 放在彼此下面,这就是它们的样子: TextView拾取字体,而开关不

我正在尝试使用。我有两个视图,一个是
TextView

<TextView
  android:layout_width="wrap_content"
  android:layout_height="wrap_content"
  android:text="Blaaah"
  android:textSize="16sp"
  app:fontFamily="@font/my_custom_font" />
放在彼此下面,这就是它们的样子:


TextView
拾取字体,而
开关不拾取字体<代码>开关
继承自
文本视图
,因此我有点不明白为什么这不起作用。如果我从代码中选择switch.setTypeface(…),字体会被很好地拾取。是否有人知道发生了什么,或者我如何从XML设置
开关的字体?

您可以通过编程更改开关组件

switchObj.setSwitchTypeface(Typeface.createFromAsset(mContext.getAssets(), "custom_font.ttf"));

这对我有用

<androidx.appcompat.widget.SwitchCompat
            android:id="@+id/switch1"
            style="@style/tvStyle_poppinsMedium_Small_Black"
            android:text="@string/push_notifications"                
            android:layout_width="match_parent"
            android:layout_height="wrap_content" />



  <style name="tvStyle_poppinsMedium_Small_Black">
        <item name="android:fontFamily">@font/poppins_medium</item>
        <item name="android:layout_width">wrap_content</item>
        <item name="android:layout_height">wrap_content</item>
        <item name="android:textColor">@color/black</item>
        <item name="android:textSize">@dimen/text_small</item>
    </style>

@字体/poppins\u中等
包装内容
包装内容
@颜色/黑色
@尺寸/文字大小

希望这会有帮助

我知道这一点,谢谢-我想知道如何从XML中实现它。FWIW
setSwitchTypeface
设置开关按钮本身的字体(开/关文本),我正在寻找标签的字体(使用
setTypeface
.Hmm,这确实改变得很好,
SwitchCompat
也不适合我,但我仍在使用旧的
android.support.v7
版本。也许是时候开始我一直推迟的“移动到AndroidX”项目了……如果我能解决这个问题,我会报告:).你用android.support.v7的switchCompat试过这个吗?我想它也能用。是的,这是我试过的方法之一,但它看起来和普通的
开关一样。我用的是Xamarin,不幸的是AndroidX软件包还没有超过RC阶段,所以我还没有准备好将它们包括在这部分中lar应用程序。让我有点惊讶的是,这在
v7
版本中不起作用,但另一方面
fontFamily
属性是一个相对较新的功能。。
<androidx.appcompat.widget.SwitchCompat
            android:id="@+id/switch1"
            style="@style/tvStyle_poppinsMedium_Small_Black"
            android:text="@string/push_notifications"                
            android:layout_width="match_parent"
            android:layout_height="wrap_content" />



  <style name="tvStyle_poppinsMedium_Small_Black">
        <item name="android:fontFamily">@font/poppins_medium</item>
        <item name="android:layout_width">wrap_content</item>
        <item name="android:layout_height">wrap_content</item>
        <item name="android:textColor">@color/black</item>
        <item name="android:textSize">@dimen/text_small</item>
    </style>