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


当前回答

非常紧凑的科特林版本:

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

其他回答

最简单的方法

TextView tv = findViewById(R.id.tv);
tv.setText("some text");
setUnderLineText(tv, "some");

还支持TextView子项,如EditText、Button、Checkbox

public void setUnderLineText(TextView tv, String textToUnderLine) {
        String tvt = tv.getText().toString();
        int ofe = tvt.indexOf(textToUnderLine, 0);

        UnderlineSpan underlineSpan = new UnderlineSpan();
        SpannableString wordToSpan = new SpannableString(tv.getText());
        for (int ofs = 0; ofs < tvt.length() && ofe != -1; ofs = ofe + 1) {
            ofe = tvt.indexOf(textToUnderLine, ofs);
            if (ofe == -1)
                break;
            else {
                wordToSpan.setSpan(underlineSpan, ofe, ofe + textToUnderLine.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
                tv.setText(wordToSpan, TextView.BufferType.SPANNABLE);
            }
        }
    }

如果你愿意

-可单击下划线文本?

-给TextView的多个部分加下划线?

然后检查此答案

     <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可绘制文件来创建底部边框,并将可绘制文件作为背景应用到文本视图中

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

转到strings.xml资源文件必要时,在资源文件中添加带有HTML下划线标记的字符串。

strings.xml HTML下划线示例

按如下方式调用Java代码中的字符串资源ID:

sampleTextView.setText(R.string.sample_string);

输出应带有下划线的单词“Stacksoverflow”。

此外,以下代码不会打印下划线:

String sampleString = getString(R.string.sample_string);
sampleTextView.setText(sampleString);

相反,使用以下代码保留RTF格式:

CharSequence sampleString = getText(R.string.sample_string);
sampleTextView.setText(sampleString);

“您可以使用getString(int)或getText(int)来检索字符串。getText(int)保留应用于字符串的任何富文本样式。”Android文档。

请参阅文档:https://developer.android.com/guide/topics/resources/string-resource.html

我希望这有帮助。