To draw an Underline below the TextView in Android

后端 未结 8 860
孤街浪徒
孤街浪徒 2020-11-29 16:39

I want to draw the underline below my TextView. I have searched a few content but couldn\'t find out anything fruitful.

Can anyone please help me out he

相关标签:
8条回答
  • 2020-11-29 16:39

    A simple and sustainable solution is to create a layer-list and make it as the background of your TextView:

    <layer-list xmlns:android="http://schemas.android.com/apk/res/android">
        <item
            android:left="-5dp"
            android:right="-5dp"
            android:top="-5dp">
            <shape>
                <stroke
                    android:width="1.5dp"
                    android:color="@color/colorAccent" />
            </shape>
        </item>
    </layer-list>
    
    0 讨论(0)
  • 2020-11-29 16:40

    You can use Kotlin Extension and type your own drawUnderLine method.

    fun TextView.drawUnderLine() {
        val text = SpannableString(this.text.toString())
        text.setSpan(UnderlineSpan(), 0, text.length, 0)
        this.text = text
    }
    
    yourTextView.drawUnderLine()
    
    0 讨论(0)
  • 2020-11-29 16:50

    5 Amazing Ways To Underline A TextView In Android - Kotlin/Java & XML

    1. String html = "<u>Underline using Html.fromHtml()</u>"; textview.setText(Html.fromHtml(html));

    But Html.fromHtml(String resource) was deprecated in API 24.

    So you can use the latest android support library androidx.core.text.HtmlCompat. Before that, you need to include the dependency in your project.

    `implementation 'androidx.core:core:1.0.1'`
    
    1. String html = "<u> 1.1 Underline using HtmlCompat.fromHtml()</u>"; //underline textview using HtmlCompat.fromHtml() method textview11.setText(HtmlCompat.fromHtml(html, HtmlCompat.FROM_HTML_MODE_LEGACY));

    Using strings.xml,

    1. <string name="underline_text">1.3 &lt;u>Underline using HtmlCompat.fromHtml() and string resource&lt;/u></string>

    textview13.setText(HtmlCompat.fromHtml(getString(R.string.underline_text), HtmlCompat.FROM_HTML_MODE_LEGACY));

    using PaintFlags

    1. textview2.setPaintFlags(textview2.getPaintFlags() | Paint.UNDERLINE_TEXT_FLAG); textview2.setText("2. Underline using setPaintFlags()");

    using SpannableString

    `String content1 = "3.1 Underline using SpannableString";
            SpannableString spannableString1 = new SpannableString(content1);
            spannableString1.setSpan(new UnderlineSpan(), 0, content1.length(), 0);
            textview31.setText(spannableString1);`
    
    0 讨论(0)
  • 2020-11-29 16:54

    If your TextView has fixed width, alternative solution can be to create a View which will look like an underline and position it right below your TextView.

    <RelativeLayout
       android:layout_width="match_parent"
       android:layout_height="match_parent">
    
            <TextView
                android:id="@+id/myTextView"
                android:layout_width="20dp"
                android:layout_height="wrap_content"/>
    
            <View
                android:layout_width="20dp"
                android:layout_height="1dp"
                android:layout_below="@+id/myTextView"
                android:background="#CCCCCC"/>
    </RelativeLayout>
    
    0 讨论(0)
  • 2020-11-29 16:55

    For anyone still looking at this querstion. This is for a hyperlink but you can modify it for just a plain underline:

    Create a drawable (hyperlink_underline.xml):

    <layer-list xmlns:android="http://schemas.android.com/apk/res/android">
      <item android:top="-10dp"
            android:left="-10dp"
            android:right="-10dp">
        <shape android:shape="rectangle">
          <solid android:color="@android:color/transparent"/>
    
          <stroke android:width="2dp"
                  android:color="#3498db"/>
        </shape>
      </item>
    </layer-list>
    

    Create a new style:

    <style name="Hyperlink">
        <item name="android:textColor">#3498db</item>
        <item name="android:background">@drawable/hyperlink_underline</item>
      </style>
    

    Then use this style on your TextView:

    <TextView
        android:layout_height="wrap_content"
        android:layout_width="wrap_content"
        local:MvxBind="Text Id; Click ShowJobInfoCommand"
        style="@style/HyperLink"/>
    
    0 讨论(0)
  • 2020-11-29 16:56

    just surround your text with < u > tag in your string.xml resource file

    <string name="your_string"><u>Underlined text</u></string>
    

    and in your Activity/Fragment

    mTextView.setText(R.string.your_string);
    
    0 讨论(0)
提交回复
热议问题