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


当前回答

现在回答这个问题已经很晚了,但是假设如果任何人想要动态获取文本,那么他们可以在java代码中使用这一行简单的代码,这行代码可以工作:

 textView.setText(Html.fromHtml("<p><u>" + get_name + "</u></p>"));

其他回答

查看带下划线的可单击按钮样式:

<TextView
    android:id="@+id/btn_some_name"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="@string/btn_add_contact"
    android:textAllCaps="false"
    android:textColor="#57a0d4"
    style="@style/Widget.AppCompat.Button.Borderless.Colored" />

字符串.xml:

<string name="btn_add_contact"><u>Add new contact</u></string>

结果:

一种更干净的方式,而不是textView.setPaintFlags(textView.getPaintFlags()|Paint.UNDERLINE_EXT_FLAG);方法是使用textView.getPaint().setUnderlineText(true);

如果您需要稍后关闭该视图的下划线,例如在RecyclerView中的重用视图中,textView.getPaint().setUnderlineText(false);

与Class一起尝试

对于java

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

Kotlin的

textview.setPaintFlags(textview.getPaintFlags() or Paint.UNDERLINE_TEXT_FLAG)

我使用这个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>

我简化了塞缪尔的回答:

<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>