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


当前回答

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

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

其他回答

非常紧凑的科特林版本:

tvTitle.apply { 
    text = "foobar"
    paint?.isUnderlineText = true
}

我简化了塞缪尔的回答:

<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
    <!--https://stackoverflow.com/a/40706098/4726718-->
    <item
        android:left="-5dp"
        android:right="-5dp"
        android:top="-5dp">
        <shape>
            <stroke
                android:width="1.5dp"
                android:color="@color/colorAccent" />
        </shape>
    </item>
</layer-list>
     <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"
                    />

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

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

你可以试试

textview.setPaintFlags(textview.getPaintFlags() |   Paint.UNDERLINE_TEXT_FLAG);