如何使用样式[duplicate]在按钮内的文本下划线

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

此问题已经在这里有了答案:

是否可以使用样式在Button中的文本下划线?我可以通过使用[html]这样的HTML标签将文本包装在strings.xml中来完成此操作>

<string name="my_text"><u>My underlined text</u></string>

但是我更喜欢这样

<com.google.android.material.button.MaterialButton
    style="@style/Widget.MaterialComponents.Button.TextButton"
    android:id="@+id/my_button"
    android:layout_width="0dp"
    android:layout_height="wrap_content"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintBottom_toBottomOf="parent"
    <!-- not working --> android:textStyle="underline"
    android:text="@string/my_text" />

理想情况下,我将在styles.xml中定义样式>

<style name="MyButton" parent="Widget.MaterialComponents.Button.TextButton">
    <!-- not working --><item name="android:textStyle">underline</item>
</style>

而且我知道我可能可以制作自己的自定义按钮,该按钮的下划线将带有文字。但这感觉要花这么多基础的功夫。

编辑

我的问题被标记为可能与this question重复,但我正在尝试通过样式实现下划线。

是否可以使用样式在Button中的文本下划线?我可以通过使用html标记将字符串包装在strings.xml中来实现,例如<不幸的是,只有这两种方式

使用<u> </u>
button.setPaintFlags(button.getPaintFlags() | Paint.UNDERLINE_TEXT_FLAG);

  • 您不能用样式定义它(underline不存在)。只需使用:
  • MaterialButton button = findViewById(R.id.xxxxx); button.setPaintFlags(button.getPaintFlags() | Paint.UNDERLINE_TEXT_FLAG);
  • enter image description here
android material-design android-xml android-button android-styles
2个回答
1
投票

0
投票
© www.soinside.com 2019 - 2024. All rights reserved.