你如何在 Android XML 中给文本加下划线?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10019001/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-20 02:12:35  来源:igfitidea点击:

How do you underline a text in Android XML?

androidtextviewandroid-stylesunderline

提问by Michael Zeuner

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

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

回答by George Artemiou

If you are using a string resource xml file (supports HTML tags), it can be done using<b> </b>, <i> </i>and <u> </u>.

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

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

If you want to underline something from code use:

如果您想从代码中强调某些内容,请使用:

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

Hope this helps

希望这可以帮助

回答by abh22ishek

Use this:

用这个:

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

回答by Bhavin

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

If it does not work then

如果它不起作用那么

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

Because "<" could be a keyword at some time.

因为“<”有时可能是关键字。

And for Displaying

并用于显示

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

回答by Jawad Zeb

First of all, go to String.xml file

首先,转到String.xml文件

you can add any HTML attributes like , italic or bold or underline here.

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

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

回答by Tharkius

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.

另一种方法是创建一个扩展 TextView 的自定义组件。这适用于需要多个带下划线的 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:

以及如何在 xml 中使用它:

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

回答by Anandharaj R

There are different ways to achieve underlined text in an Android TextView.

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

1.<u>This is my underlined text</u>or

1.<u>This is my underlined text</u>

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

2.You can do the same programmatically.

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

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

3.It can be done by creating a SpannableString and then setting it as the TextView text property

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

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

回答by MazRoid

If you want to compare text String or the text will change dynamically then you can created a view in Constraint layout it will adjust according to text length like this

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

 <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>

回答by Anthone

To complete Bhavin answer. For exemple, to add underline or redirection.

完成 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>

and you can know compatible tag here : http://commonsware.com/blog/Android/2010/05/26/html-tags-supported-by-textview.html

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

回答by Hitesh Dhamshaniya

I used below method, it worked for me. Below is example for Button but we can use in TextView as well.

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

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

回答by Tebo

You can use the markup below, but note that if you set the textAllCapsto truethe underline effect would be removed.

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

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



Note

笔记

Using textAllCaps with a string (login_change_settings) that contains markup; the markup will be dropped by the caps conversion

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

The textAllCaps text transform will end up calling toString on the CharSequence, which has the net effect of removing any markup such as . This check looks for usages of strings containing markup that also specify textAllCaps=true.

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