正如其他人所说,您可以使用 textAlignment ...但我想告诉您,如果您使用自定义字体,它将不会完全垂直对齐。你可以在这里查看解释。
所以我会制作一个继承自芯片样式的自定义样式,并设置字体填充以供使用,如下所示:
<style name="customStyle" parent="@style/Widget.MaterialComponents.Chip.Choice">
<item name="chipBackgroundColor">@color/white</item>
******* <item name="android:includeFontPadding">true</item> *************
</style>
然后对于文本外观,您可以制作另一种样式:
<style name="CustomChipTextAppearance" parent="TextAppearance.MaterialComponents.Chip">
<item name="android:fontFamily">?attr/myFont</item>
<item name="android:textAlignment">center</item>
</style>
不要忘记在 xml 中强制设置桥接主题:
<com.google.android.material.chip.Chip xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
style="@style/customStyle"
**** android:theme="@style/Theme.MaterialComponents.Bridge" ****
***** android:textAppearance="@style/CustomChipTextAppearance" *******
app:chipMinHeight="38dp"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
app:chipStrokeWidth="2dp"
app:rippleColor="@android:color/transparent"
tools:chipText="my chip" />