How to set underline text on textview?

后端 未结 12 1171
慢半拍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 17:49

    I believe that you need to use CharSequence. You can find an example here.

    0 讨论(0)
  • 2020-12-23 17:49

    You can easily add a View with the height of 1dp aligning start and the end of TextView, right below it. You can also use marginTop with negative value to make the underline closer to TextView. If you use your TextView and the under inside a relative layout, it would be much easier for alignments. here is the example:

    <RelativeLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content">
    
    <View
      android:id="@+id/viewForgotPasswordUnderline"
      android:layout_width="wrap_content"
      android:layout_height="1dp"
      android:layout_below="@id/txtForgotPassword"
      android:layout_alignStart="@id/txtForgotPassword"
      android:layout_alignEnd="@id/txtForgotPassword"
      android:layout_marginTop="-2dp"
      android:background="@color/lightGray" />
    
    <androidx.appcompat.widget.AppCompatTextView
      android:id="@+id/txtForgotPassword"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:layout_centerHorizontal="true"
      android:paddingTop="8dp"
      android:text="@string/userLoginForgotPassword"
      android:textColor="@color/lightGray"
      android:textSize="16dp" />
    </RelativeLayout>
    
    0 讨论(0)
  • 2020-12-23 17:52

    Android supports string formatting using HTML tags in the strings xml. So the following would work:

    <string name="label_testinghtmltags"><u>This text has an underscore</u></string>
    

    Then you just use it as you normally would. You can see more in the documentation here

    Update:

    To further expand on the answer, the documentation says:

    ... the format(String, Object...) and getString(int, Object...) methods strip all the style information from the string. . The work-around to this is to write the HTML tags with escaped entities, which are then recovered with fromHtml(String), after the formatting takes place

    For example, the resulting string from the following code snippet will not show up as containing underscored elements:

    <string name="html_bold_test">This <u>%1$s</u> has an underscore</string>
    String result = getString(R.string.html_bold_test, "text")
    

    However the result from the following snippet would contain underscores:

    <string name="html_bold_test">This &lt;u> %1$s &lt;/u> has an underscore</string>
    String result = Html.fromHtml(getString(R.string.html_bold_test, "text"))
    
    0 讨论(0)
  • 2020-12-23 17:54

    Try this,

    tv.setPaintFlags(Paint.UNDERLINE_TEXT_FLAG);
    
    0 讨论(0)
  • 2020-12-23 17:58

    Use this

    tvHide.setPaintFlags(tvHide.getPaintFlags() | Paint.UNDERLINE_TEXT_FLAG);
    
    0 讨论(0)
  • You can do it like:

    tvHide.setText(Html.fromHtml("<p><span style='text-decoration: underline'>Hide post</span></p>").toString());
    

    Hope this helps

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