How to set underline text on textview?

后端 未结 12 1173
慢半拍i
慢半拍i 2020-12-23 17:42

How to set underline text on textview?

I have used following code but it is not working.

tvHide.setText(Html.fromHtml("

相关标签:
12条回答
  • 2020-12-23 18:04

    you're almost there: just don't call toString() on Html.fromHtml() and you get a Spanned Object which will do the job ;)

    tvHide.setText(Html.fromHtml("<p><u>Hide post</u></p>"));
    
    0 讨论(0)
  • 2020-12-23 18:04

    Need not to use HTML properties, let's focus on java xD I had the same problem, i found the way to do it in java:

    String text="Hide post";
    TextView tvHide=(TextView)findViewById(R.id.text);
    SpannableString spanString = new SpannableString(text);
    spanString.setSpan(new UnderlineSpan(), 0, spanString.length(), 0);
    tvHide.setText(spanString );
    
    0 讨论(0)
  • 2020-12-23 18:09

    Simple and easy way to display underline in TextView is:

    yourTextView.getPaint().setUnderlineText(true);
    

    And if you want to remove underlining for that view, you can write below code:

    yourTextView.getPaint().setUnderlineText(false);

    It will work even though you set android:textAllCaps="true"

    0 讨论(0)
  • 2020-12-23 18:10

    You have to use SpannableString for that :

    String mystring=new String("Hello.....");
    SpannableString content = new SpannableString(mystring);
    content.setSpan(new UnderlineSpan(), 0, mystring.length(), 0);
    yourtextview.setText(content);
    

    Update : You can refer my answer on Underling TextView's here in all possible ways.

    0 讨论(0)
  • 2020-12-23 18:13

    Following are the some approaches for underlined text in android:

    1st Approach

    You can define your string in strings.xml

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

    And use that string in your xml file

    <TextView
                android:id="@+id/txt_underlined"
                android:layout_width="wrap_content"
                android:layout_height="match_parent"
                android:text="@string/your_string"/>
    

    Or you can use that string in your Activity/Fragment

    txtView.setText(R.string.your_string);
    

    2nd Approach

    To underline the text in TextView, you can use SpannableString

    String text="Underlined Text";
    SpannableString content = new SpannableString(text);
    content.setSpan(new UnderlineSpan(), 0, text.length(), 0);
    txtView.setText(content);
    

    3rd Approach

    You can make use of setPaintFlags method of TextView to underline the text of TextView.

    txtView.setPaintFlags(mTextView.getPaintFlags() | Paint.UNDERLINE_TEXT_FLAG);
    txtView.setText("Underlined Text");
    

    4th Approach

    Make use of Html.fromHtml(htmlString);

    String htmlString="<u>Underlined Text</u>";
    txtView.setText(Html.fromHtml(htmlString));
    

    Or

    txtView.setText(Html.fromHtml("<u>underlined</u> text"));
    

    Note:

    If you have added this line android:textAllCaps="true" in your layout, then none of the above will work. For that, you have to define your string in Caps and then any of the above approach.

    0 讨论(0)
  • 2020-12-23 18:13

    In my sign_in.xml file I used this:

    <Button
        android:id="@+id/signuptextlinkbtn"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="10dp"
        android:background="@android:color/transparent"
        android:onClick="performSingUpMethod"
        android:text="Sign up now!"
        android:textColor="#5B55FF"
        android:textSize="15dp"
        android:textStyle="bold" />
    

    In my SignIn.java file, I used this:

    import android.graphics.Paint;
    Button signuptextlinkbutton = (Button) findViewById(R.id.signuptextlinkbtn);
    signuptextlinkbutton.setPaintFlags(signuptextlinkbutton.getPaintFlags()| Paint.UNDERLINE_TEXT_FLAG);
    

    Now I can see my link underlined. It was simply text even thought I declared it as a Button.

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