你如何在Android XML中强调文本?

Mic*_*ner 100 android underline textview android-styles

如何在XML文件中为文本加下划线?我找不到一个选项textStyle.

Geo*_*iou 163

如果您使用的是字符串资源的XML文件(支持HTML标签),这是可以做到用<b> </b>,<i> </i><u> </u>.

<resources>
    <string name="your_string_here">
        This is an <u>underline</u>.
    </string>
</resources>
Run Code Online (Sandbox Code Playgroud)

如果你想用代码使用下划线:

TextView tv = (TextView) view.findViewById(R.id.tv);
SpannableString content = new SpannableString("Content");
content.setSpan(new UnderlineSpan(), 0, content.length(), 0);
tv.setText(content);
Run Code Online (Sandbox Code Playgroud)

希望这可以帮助

  • 在我的情况下,android studio预览无法确定html标签.但是一旦我在真实设备上运行项目,下划线的文字就显得很愉快. (9认同)
  • 我试过这个.字符串没有加下划线? (8认同)
  • 我遇到了通过`<u>`标签不起作用的情况,例如有时如果你使用自定义字体.但是,由UnderlineSpan编程的底层确实从未在我身上失败过,所以我推荐它作为最可靠的解决方案. (4认同)

abh*_*hek 55

用这个:

TextView txt = (TextView) findViewById(R.id.Textview1);
txt.setPaintFlags(txt.getPaintFlags() | Paint.UNDERLINE_TEXT_FLAG);
Run Code Online (Sandbox Code Playgroud)


Bha*_*vin 21

<resource>
    <string name="your_string_here">This is an <u>underline</u>.</string>
</resources>
Run Code Online (Sandbox Code Playgroud)

如果它不起作用那么

<resource>
<string name="your_string_here">This is an &lt;u>underline&lt;/u>.</string>
Run Code Online (Sandbox Code Playgroud)

因为"<"可能在某个时候成为关键字.

并用于显示

TextView textView = (TextView) view.findViewById(R.id.textview);
textView.setText(Html.fromHtml(getString(R.string.your_string_here)));
Run Code Online (Sandbox Code Playgroud)

  • 这有效...但如果由于某种原因你将TextView设置为使用xml属性android:textAllCaps ="true",则不会出现下划线.删除此修饰符,下划线将按预期显示.只是一个抬头:) (3认同)

Xar*_*mer 9

首先,转到String.xml文件

您可以在此处添加任何 HTML 属性,例如 、斜体、粗体或下划线。

    <resources>
        <string name="your_string_here">This is an <u>underline</u>.</string>
    </resources>
Run Code Online (Sandbox Code Playgroud)


Hit*_*iya 5

我使用了下面的方法,它对我有用。下面是 Button 的示例,但我们也可以在 TextView 中使用。

Button btnClickMe = (Button) findViewById(R.id.btn_click_me);
btnClickMe.setPaintFlags(btnClickMe.getPaintFlags() | Paint.UNDERLINE_TEXT_FLAG);
Run Code Online (Sandbox Code Playgroud)


Tha*_*ius 5

另一种方法是创建一个扩展 TextView 的自定义组件。这对于需要有多个带下划线的 TextView 的情况很有用。

这是该组件的代码:

package com.myapp.components;

import android.content.Context;
import android.support.v7.widget.AppCompatTextView;
import android.text.SpannableString;
import android.text.style.UnderlineSpan;
import android.util.AttributeSet;

public class LinkTextView extends AppCompatTextView {
    public LinkTextView(Context context) {
        this(context, null);
    }

    public LinkTextView(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    @Override
    public void setText(CharSequence text, BufferType type) {
        SpannableString content = new SpannableString(text);
        content.setSpan(new UnderlineSpan(), 0, content.length(), 0);

        super.setText(content, type);
    }
}
Run Code Online (Sandbox Code Playgroud)

以及如何在 xml 中使用它:

<com.myapp.components.LinkTextView
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:text="Hello World!" />
Run Code Online (Sandbox Code Playgroud)