How do you underline a text in Android XML?

前端 未结 10 983
不思量自难忘°
不思量自难忘° 2020-12-04 14:57

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

相关标签:
10条回答
  • 2020-12-04 15:23
    <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)));
    
    0 讨论(0)
  • 2020-12-04 15:23

    First of all, go to String.xml file

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

        <resources>
            <string name="your_string_here">This is an <u>underline</u>.</string>
        </resources>
    
    0 讨论(0)
  • 2020-12-04 15:32

    Use this:

    TextView txt = (TextView) findViewById(R.id.Textview1);
    txt.setPaintFlags(txt.getPaintFlags() | Paint.UNDERLINE_TEXT_FLAG);
    
    0 讨论(0)
  • 2020-12-04 15:33

    I used below method, it worked 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);
    
    0 讨论(0)
  • 2020-12-04 15:37

    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!" />
    
    0 讨论(0)
  • 2020-12-04 15:46

    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

    0 讨论(0)
提交回复
热议问题