How do you underline a text in Android XML?

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

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

10条回答
  •  萌比男神i
    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:

    
    

提交回复
热议问题