How do you underline a text in Android XML?

2019-01-13 12:08发布

How do you underline a text in an XML file? I can't find an option in textStyle.

9条回答
三岁会撩人
2楼-- · 2019-01-13 12:11

First of all go to String.xml file

you can add any html attributes like , italic or bold or underline here.

    <resource>
        <string name="your_string_here">This is an <u>underline</u>.</string>
    </resources>
查看更多
Viruses.
3楼-- · 2019-01-13 12:16

If you are using a string resource xml file (supports HTML tags), it can be done using<b> </b>, <i> </i> and <u> </u>.

<resources>
    <string name="your_string_here">
        This is an <u>underline</u>.
    </string>
</resources>

If you want to underline something from code use:

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

Hope this helps

查看更多
霸刀☆藐视天下
4楼-- · 2019-01-13 12:16

I used below method, it's work for me. below is example for Button but we can use in TextView as well.

Button btnClickMe= (Button) findViewById(R.id.btn_click_me);
btnClickMe.setPaintFlags(btnClickMe.getPaintFlags() | Paint.UNDERLINE_TEXT_FLAG);
查看更多
聊天终结者
5楼-- · 2019-01-13 12:25
<resource>
    <string name="your_string_here">This is an <u>underline</u>.</string>
</resources>

If it does not work then

<resource>
<string name="your_string_here">This is an &lt;u>underline&lt;/u>.</string>

Because "<" could be a keyword at some time.

And for Displaying

TextView textView = (TextView) view.findViewById(R.id.textview);
textView.setText(Html.fromHtml(getString(R.string.your_string_here)));
查看更多
仙女界的扛把子
6楼-- · 2019-01-13 12:28

Another way to do it, is creating a custom component extending TextView. It's good for cases where you need to have multiple underlined TextViews.

Here's the code for the component:

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);
    }
}

And how to use it in xml:

<com.myapp.components.LinkTextView
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:text="Hello World!" />
查看更多
Explosion°爆炸
7楼-- · 2019-01-13 12:28

There are different ways to achieve underlined text in an Android TextView.

1.<u>This is my underlined text</u> or

I just want to underline <u>this</u> word

2.You can do the same programmatically.

`textView.setPaintFlags(textView.getPaintFlags() | Paint.UNDERLINE_TEXT_FLAG);`

3.It can be done by creating a SpannableString and then setting it as the TextView text property

SpannableString text = new SpannableString("Voglio sottolineare solo questa parola");
text.setSpan(new UnderlineSpan(), 25, 6, 0);
textView.setText(text);
查看更多
登录 后发表回答