8

我想从设计支持库更改芯片视图的字体。在我浏览它的源代码后,我发现它直接通过Paint在ChipDrawable中绘制文本,我找不到任何公共方法来更改字体。我应该如何尝试更改 Chip 视图字体?请帮忙。谢谢。

4

3 回答 3

26

有点晚了,但如果有人仍然感兴趣,我会发布我的解决方案。

首先使用您的自定义字体创建一个字体系列 xml。

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

然后将芯片样式添加到使用您的字体系列的样式 xml 中:

<style name="chipText" parent="TextAppearance.MaterialComponents.Chip">
    <item name="android:textSize">14sp</item>
    <item name="android:fontFamily">@font/my_font_family</item>
</style>

然后,当您在 xml 中创建芯片视图时,设置 textAppearance 属性:

android:textAppearance="@style/chipText"
于 2019-06-05T14:48:02.063 回答
0

您的解决方案工作正常芯片视图 - TextInputLayout 内仍有一些问题芯片。(字体不变)

笔记:屏幕旋转后自定义字体改变了,不知道为什么

实施“com.google.android.material:material:1.3.0-alpha01”

   <com.google.android.material.textfield.TextInputLayout
        android:layout_width="240dp"
        android:layout_height="wrap_content"
        style="@style/Widget.MaterialComponents.TextInputLayout.OutlinedBox"
        android:layout_gravity="center"
        android:textAppearance="@style/chipText"
        android:hint="Text Field">

        <com.google.android.material.textfield.TextInputEditText
            android:id="@+id/editText"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:text="Text"
            android:textAppearance="@style/chipText"/>

    </com.google.android.material.textfield.TextInputLayout>
于 2020-06-24T18:22:18.827 回答
0

用这个

chip.typeface = Typeface.create(ResourcesCompat.getFont(this,R.font.segoe_ui),Typeface.NORMAL)
于 2021-09-28T10:20:29.947 回答