button.setPaintFlags(button.getPaintFlags() | Paint.UNDERLINE_TEXT_FLAG);
此问题已经在这里有了答案:
是否可以使用样式在Button中的文本下划线?我可以通过使用[html]这样的HTML标签将文本包装在strings.xml
中来完成此操作>
<string name="my_text"><u>My underlined text</u></string>
但是我更喜欢这样
我的问题被标记为可能与this question重复,但我正在尝试通过样式实现下划线。<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>
而且我知道我可能可以制作自己的自定义按钮,该按钮的下划线将带有文字。但这感觉要花这么多基础的功夫。
编辑
是否可以使用样式在Button中的文本下划线?我可以通过使用html标记将字符串包装在strings.xml中来实现,例如