如何在Android布局xml文件中定义带下划线的文本?


当前回答

只需使用字符串资源文件中的属性,例如。

<string name="example"><u>Example</u></string>

其他回答

要在Kotlin做到这一点:

你的TextView.praint?。isUnderlineText=真

我使用这个xml可绘制文件来创建底部边框,并将可绘制文件作为背景应用到文本视图中

<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
    <item>
        <shape android:shape="rectangle" >
            <solid android:color="@android:color/transparent" />
        </shape>
    </item>

    <item android:top="-5dp" android:right="-5dp" android:left="-5dp">
        <shape>
            <solid android:color="@android:color/transparent" />
            <stroke
                    android:width="1.5dp"
                    android:color="@color/pure_white" />
        </shape>
    </item>
</layer-list>

罗曼·盖伊(Romain Guy)用GitHub上的可用源代码描述了最新的画下划线文本的方法。此示例应用程序公开了两种可能的实现:

需要API级别19的基于路径的实现需要API级别1的基于区域的实现

     <com.google.android.material.textfield.TextInputEditText
                    android:layout_width="0dp"
                    android:layout_height="match_parent"
                    android:layout_weight="1"
                    android:backgroundTint="@android:color/transparent"
                    android:hint="@string/search_url"
                    android:textColor="@color/coffee_color"
                    android:textColorHint="@color/coffee_color"
                    />

如果要在XML中实现这一点,请在resource中声明字符串,并将该资源值放入HTML的下划线标记(<u></u>)中。在TextView中,添加

android:text="@string/your_text_reference"

在字符串资源值中,

<string name="your_text_reference"><u>Underline me</u></string>

如果您想以编程方式实现这一点,请使用Kotlin

textView.paintFlags = textView.paintFlags or Paint.UNDERLINE_TEXT_FLAG

or,

textView.text = Html.fromHtml("<p><u>Underline me</u></p>")