【问题标题】:How do you underline a text in Android XML?如何在 Android XML 中为文本添加下划线?
【发布时间】:2012-04-18 15:01:53
【问题描述】:

如何在 XML 文件中为文本添加下划线?我在textStyle 中找不到选项。

【问题讨论】:

  • 你已经尝试使用style="text-decoration: underline;"
  • 你能完整地使用它吗?
  • 你试过这个textView.setText(Html.fromHtml("<u>"+"testest"+"</u>"));

标签: android textview android-styles underline


【解决方案1】:

如果您使用的是字符串资源xml文件(支持HTML标签),可以使用<b> </b><i> </i><u> </u>来完成。

<resources>
    <string name="your_string_here">
        This is an <u>underline</u>.
    </string>
</resources>

如果你想在代码中使用下划线:

TextView tv = (TextView) view.findViewById(R.id.tv);
SpannableString content = new SpannableString("Content");
content.setSpan(new UnderlineSpan(), 0, content.length(), 0);
tv.setText(content);

希望对你有帮助

【讨论】:

  • 我试过了。字符串没有出现下划线?
  • 我遇到过通过&lt;u&gt; 标记不起作用的情况,例如有时,如果您使用的是自定义字体。但是,UnderlineSpan 以编程方式进行的底层对我来说确实从未失败过,所以我会推荐它作为最可靠的解决方案。
  • 您不需要在 Java 中设置文本。只需在 XML 中使用&lt;u&gt;&lt;\u&gt;,就足够了。
  • 在我的情况下,android studio preview 无法确定 html 标签。但是一旦我在真实设备上运行该项目,带下划线的文本就会愉快地显示出来。
  • 考虑到我们已经使用 Android 超过 5 年,当 Google 已经为我们提供粗体/斜体/正常时,您必须将字符串转换为 HTML 的事实是相当松懈...
【解决方案2】:

使用这个:

TextView txt = (TextView) findViewById(R.id.Textview1);
txt.setPaintFlags(txt.getPaintFlags() | Paint.UNDERLINE_TEXT_FLAG);

【讨论】:

  • 请注意,此解决方案总是在整个文本下划线,因此如果只想在其中的一部分下划线是不可行的。为此,您需要UnderlineSpan
【解决方案3】:
<resource>
    <string name="your_string_here">This is an <u>underline</u>.</string>
</resources>

如果还是不行的话

<resource>
<string name="your_string_here">This is an &lt;u>underline&lt;/u>.</string>

因为“

用于显示

TextView textView = (TextView) view.findViewById(R.id.textview);
textView.setText(Html.fromHtml(getString(R.string.your_string_here)));

【讨论】:

  • 这行得通...但是如果由于某种原因您将 TextView 设置为使用 xml 属性 android:textAllCaps="true",则不会出现下划线。删除此修饰符,下划线将按预期显示。只是提醒:)
【解决方案4】:

首先,进入String.xml文件

您可以在此处添加任何 HTML 属性,如斜体、粗体或下划线。

    <resources>
        <string name="your_string_here">This is an <u>underline</u>.</string>
    </resources>

【讨论】:

    【解决方案5】:

    您可以使用下面的标记,但请注意,如果您将textAllCaps 设置为true,下划线效果将被移除。

    <resource>
        <string name="my_string_value">I am <u>underlined</u>.</string>
    </resources>
    


    注意

    将 textAllCaps 与包含标记的字符串 (login_change_settings) 一起使用;标记将被大写转换丢弃

    textAllCaps 文本转换最终会在 CharSequence 上调用 toString,这具有删除任何标记(例如 .此检查查找包含同时指定 textAllCaps=true 的标记的字符串的用法。

    【讨论】:

      【解决方案6】:

      另一种方法是创建一个扩展 TextView 的自定义组件。这适用于需要多个带下划线的 TextView 的情况。

      这是组件的代码:

      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);
          }
      }
      

      以及如何在xml中使用:

      <com.myapp.components.LinkTextView
          android:layout_width="match_parent"
          android:layout_height="wrap_content"
          android:text="Hello World!" />
      

      【讨论】:

        【解决方案7】:

        我使用了下面的方法,它对我有用。下面是 Button 的示例,但我们也可以在 TextView 中使用。

        Button btnClickMe = (Button) findViewById(R.id.btn_click_me);
        btnClickMe.setPaintFlags(btnClickMe.getPaintFlags() | Paint.UNDERLINE_TEXT_FLAG);
        

        【讨论】:

          【解决方案8】:

          完成 Bhavin 的回答。 例如,添加下划线或重定向。

          ((TextView) findViewById(R.id.tv_cgu)).setText(Html.fromHtml(getString(R.string.upload_poi_CGU)));
          
          <string name="upload_poi_CGU"><![CDATA[ J\'accepte les <a href="">conditions générales</a>]]></string>
          

          你可以在这里知道兼容的标签: http://commonsware.com/blog/Android/2010/05/26/html-tags-supported-by-textview.html

          【讨论】:

            【解决方案9】:

            有多种方法可以在 Android TextView 中实现带下划线的文本。

            1.&lt;u&gt;This is my underlined text&lt;/u&gt;

            I just want to underline <u>this</u> word
            

            2.您可以以编程方式执行相同操作。

            `textView.setPaintFlags(textView.getPaintFlags() | Paint.UNDERLINE_TEXT_FLAG);`
            

            3.可以通过创建一个 SpannableString 然后将其设置为 TextView 文本属性来完成

            SpannableString text = new SpannableString("Voglio sottolineare solo questa parola");
            text.setSpan(new UnderlineSpan(), 25, 6, 0);
            textView.setText(text);
            

            【讨论】:

              【解决方案10】:

              如果你想比较文本字符串或者文本会动态变化,那么你可以在约束布局中创建一个视图,它会像这样根据文本长度进行调整

               <android.support.constraint.ConstraintLayout
                  android:layout_width="match_parent"
                  android:layout_height="wrap_content">
              
                  <TextView
                      android:id="@+id/txt_Previous"
                      android:layout_width="wrap_content"
                      android:layout_height="wrap_content"
                      android:layout_marginStart="16dp"
                      android:layout_marginLeft="16dp"
                      android:layout_marginEnd="16dp"
                      android:layout_marginRight="16dp"
                      android:layout_marginBottom="8dp"
                      android:gravity="center"
                      android:text="Last Month Rankings"
                      android:textColor="@color/colorBlue"
                      android:textSize="15sp"
                      android:textStyle="bold"
                      app:layout_constraintBottom_toBottomOf="parent"
                      app:layout_constraintEnd_toEndOf="parent"
                      app:layout_constraintStart_toStartOf="parent" />
              
                  <View
                      android:layout_width="0dp"
                      android:layout_height="0.7dp"
                      android:background="@color/colorBlue"
                      app:layout_constraintEnd_toEndOf="@+id/txt_Previous"
                      app:layout_constraintStart_toStartOf="@+id/txt_Previous"
                      app:layout_constraintBottom_toBottomOf="@id/txt_Previous"/>
              
              
              </android.support.constraint.ConstraintLayout>
              

              【讨论】:

                【解决方案11】:

                创建字符串资源:

                &lt;string name="HEADER_DELTA"&gt;&lt;b&gt;&lt;u&gt;DELTA&lt;/u&gt;&lt;/b&gt;&lt;/string&gt;

                并添加到您的 Textview

                    <TextView
                        android:id="@+id/txtDeltaText"
                        style="@style/Default_TextBox_Small"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:layout_marginTop="4dp"
                        android:gravity="center_horizontal"
                        android:text="@string/HEADER_DELTA"
                        app:layout_constraintEnd_toEndOf="parent"
                        app:layout_constraintStart_toEndOf="@+id/txtActualMetric"
                        app:layout_constraintTop_toBottomOf="@+id/txtMetricName" />
                

                【讨论】:

                  猜你喜欢
                  • 2011-04-13
                  • 1970-01-01
                  • 1970-01-01
                  • 2012-09-07
                  • 2013-02-21
                  • 2015-10-21
                  • 2015-05-29
                  • 1970-01-01
                  • 1970-01-01
                  相关资源
                  最近更新 更多