从材料零部件上的按钮上删除等宽字体

Fil*_*oli 10 android material-components material-components-android

如何使用新的材质组件从按钮中删除等宽字体?

<com.google.android.material.button.MaterialButton
        android:id="@+id/btn_register"
        style="@style/Widget.MaterialComponents.Button"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_marginBottom="8dp"
        app:fontFamily="@font/lato"
        app:backgroundTint="@color/white"
        android:textColor="?colorPrimary"
        android:text="Open Register Screen" />
Run Code Online (Sandbox Code Playgroud)

此图显示了我要消除的差异:

Fil*_*oli 16

我发现了问题。它不是等宽字体,而是letterSpacing。因此,我只需添加android:letterSpacing="0"按钮即可解决。


Cam*_*ham 7

要全局更新所有Button的字母间距,应使用以下主题:https://material.io/develop/android/theming/typography/

您可以在主题中重新定义?attr / textAppearanceButton,以指向具有所需letterSpacing的其他文本外观。

像这样在主题中定义attr:

<style name="Theme.MyApp" parent="Theme.MaterialComponents.Light">
    <item name="textAppearanceButton">@style/TextAppearance.MyApp.Button</item>
</style>
Run Code Online (Sandbox Code Playgroud)

并创建一个新的TextAppearance样式:

<style name="TextAppearance.MyApp.Button" parent="TextAppearance.MaterialComponents.Button">
  <item name="android:letterSpacing">0</item>
</style>
Run Code Online (Sandbox Code Playgroud)