【发布时间】:2018-06-06 16:50:14
【问题描述】:
我无法获得按钮来使用我的自定义字体。 它适用于 TextView,而不是 Button(它是 TextView 的子类)。
我正在使用带有 minSdkVersion 26 和 Kotlin 的 Android Studio 3.1.2。
res/font/font.xml
<?xml version="1.0" encoding="utf-8"?>
<font-family xmlns:android="http://schemas.android.com/apk/res/android">
<font
android:fontStyle="normal"
android:fontWeight="400"
android:font="@font/my-custom-font" />
</font-family>
res/layout/fragment.xml
...
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/myButton"
android:maxWidth="30dp"
android:minWidth="30dp"
android:maxHeight="40dp"
android:minHeight="40dp"
android:fontFamily="@font/my-custom-font"
android:textSize="20dp"
android:text="a" />
...
以上工作。将 TextView 替换为 Button 并停止工作。
编辑:我想使用自定义字体来保存按钮的图标,而不是使用图像和 ImageButtons。我们发现自定义字体比图像更易于管理。
【问题讨论】:
-
按钮不是默认粗体吗?您的字体没有提供粗体样式
-
@SamuelEminet 根据Fonts documentation 我只能将
fontStyle设置为normal或italic -
要添加粗体样式,请添加粗细约为 700 的字体
android:fontWeight="700" -
感谢@SamuelEminet,但我不想让它变粗,只是为了使用我的自定义字体。如果我为 Button 使用标准字体,它会以正常重量显示。
标签: android button kotlin textview custom-font