簡體   English   中英

在Xamarin Android中為按鈕文本設置自定義字體

[英]set a custom font for a button text in Xamarin Android

我有一個按鈕,我想為文本設置自定義字體,但是這樣做很麻煩。

代碼如下:

在.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>

而不是創建其他樣式,應在按鈕樣式中使用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>

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM