在Xamarin Android中为按钮文本设置自定义字体

问题描述 投票:0回答:1

我有一个按钮,我想为文本设置自定义字体,但我很难做到这一点。

代码如下:

在.xml文件中:

<Button
        android:id="@+id/button"
        style="@style/ButtonStyle"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        tools:text="Sample Text"/>

并在styles.xml中

<style name="ButtonStyle" parent="@style/Widget.AppCompat.Button.Borderless">
        <item name="android:textSize">@dimen/button_text_size</item>
        <item name="android:background">@drawable/button_background</item>
        <item name="android:textAppearanceButton">@style/ButtonTextStyle</item>
        <item name="android:textColor">?android:textColorPrimaryInverse</item>
    </style>

<style name="ButtonTextStyle" parent="TextAppearance.AppCompat">
        <item name="android:fontFamily">@font/my_custom_font</item>
    </style>
android xamarin xamarin.android
1个回答
0
投票

应该在按钮样式中使用fontFamily而不是创建不同的样式,代码应该如下所示。

<style name="ButtonStyle" parent="@style/Widget.AppCompat.Button.Borderless">
    <item name="android:textSize">@dimen/button_text_size</item>
    <item name="android:background">@drawable/button_background</item>
    <item name="android:fontFamily">@font/my_custom_font</item>
    <item name="android:textColor">?android:textColorPrimaryInverse</item>
</style>
© www.soinside.com 2019 - 2024. All rights reserved.