【问题标题】:Can't set custom font to the Button无法为按钮设置自定义字体
【发布时间】:2021-06-03 00:14:08
【问题描述】:

我有一个按钮,像这样使用<android.widget.Button,因为需要设置自定义背景(read more about issue with DayNight theme 和按钮)。接下来我需要设置自定义字体,请参阅下面的样式。在 Studio 设计器面板中,自定义字体看起来不错,但在设备上运行时使用的是 Roboto(默认)字体,而不是我的自定义字体(Nunito)。有什么建议? AS4.1.2/API30

<style name="ButtonGetStarted" parent="Widget.MaterialComponents.Button">
    <item name="android:layout_width">match_parent</item>
    <item name="android:layout_height">60dp</item>
    <item name="android:textAllCaps">false</item>
    <item name="android:textSize">24sp</item>
    <item name="android:textStyle">bold</item>
    <item name="android:textColor">@color/text_invert</item>
    <item name="android:fontFamily">@font/app_font</item>
    <item name="android:background">@drawable/button_selector_get_started</item>
</style>

app_font.xml:

<font-family xmlns:app="http://schemas.android.com/apk/res-auto">    
    <font
        app:fontStyle="normal"
        app:font="@font/nunito_regular" />

    <font
        app:fontStyle="italic"
        app:font="@font/nunito_regular_italic" />

</font-family>

【问题讨论】:

    标签: android material-design


    【解决方案1】:

    创建一个 TextAppearance 这里的例子

     <style name="TextAppearance.Robin.Button" parent="TextAppearance.MaterialComponents.Button">
                <item name="fontFamily">@font/dosis_medium</item>
                    <item name="android:fontFamily">@font/dosis_medium</item>
                    <item name="android:textSize">15sp</item> 
    </style>
    

    现在在您的按钮样式中设置 TextAppearance

    <style name="ButtonGetStarted" parent="Widget.MaterialComponents.Button">
     <item name="android:textAppearance">@style/TextAppearance.Robin.Button</item>
       .
       .
       .
       .
    </style>
    

    here reference to the MDC guide

    你也可以使用按钮本身

    <Button
    android:textAppearance:"android:textAppearance="@style/yourstYle
    />
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-10-18
      • 2016-05-26
      • 1970-01-01
      • 1970-01-01
      • 2014-06-16
      • 1970-01-01
      • 2014-09-11
      • 1970-01-01
      相关资源
      最近更新 更多