我该如何定义 下划线的 Android 布局中的文本 xml 文件?

有帮助吗?

解决方案

可以如果使用的是一个实现字符串资源 xml文件,它支持HTML标签等<b></b><i></i><u></u>

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

如果您想从代码中使用下划线的东西:

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

其他提示

您可以尝试

textview.setPaintFlags(textview.getPaintFlags() |   Paint.UNDERLINE_TEXT_FLAG);

在 “接受” 的答案上面确实不会工作(当您尝试使用像textView.setText(Html.fromHtml(String.format(getString(...), ...)))的字符串。

如在单证指出必须转义(HTML实体编码的)开口托架与&lt;内标签,例如结果应该是这样的:

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

然后在你的代码,你可以设置文本:

TextView textView = (TextView) view.findViewById(R.id.textview);
textView.setText(Html.fromHtml(String.format(getString(R.string.my_string), ...)));

strings.xml档案内容:

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

布局xml文件shold使用上面的字符串资源与下面的TextView的性质,如下所示:

<TextView 
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:gravity="center_horizontal"
    android:text="@string/my_text"

    android:selectAllOnFocus="false"
    android:linksClickable="false"
    android:autoLink="all"
    />

有关按钮和TextView的,这是最简单的方法:

按钮:

Button button = (Button) findViewById(R.id.btton1);
button.setPaintFlags(button.getPaintFlags() | Paint.UNDERLINE_TEXT_FLAG);

的TextView:

TextView textView = (TextView) findViewById(R.id.textview1);
textView.setPaintFlags(textView.getPaintFlags() | Paint.UNDERLINE_TEXT_FLAG);

一行溶液

myTextView.setText(Html.fromHtml("<p><u>I am Underlined text</u></p>"));

这是有点晚了,但可能是有用的人。

Romain Guy 描述了绘制带下划线文本的最新方法 中等的 具有可用的源代码 GitHub。该示例应用程序公开了两种可能的实现:

  • 需要 API 级别 19 的基于路径的实现
  • 需要 API 级别 1 的基于区域的实现

enter image description here

在科特林扩展功能可以使用。这只能从代码中使用,而不是XML。

fun TextView.underline() {
    paintFlags = paintFlags or Paint.UNDERLINE_TEXT_FLAG
}

用法:

 tv_change_number.underline()
 tv_resend_otp.underline()

我知道这是一个迟到的答案,但我想出了非常有效的解决方案......我从安东尼Forloney的答案强调在代码文本和创建TextView中的一个子类来处理,对你。然后,每当你想拥有一个带下划线的TextView你可以使用XML格式的子类。

下面是我创建的类:

import android.content.Context;
import android.text.Editable;
import android.text.SpannableString;
import android.text.TextWatcher;
import android.text.style.UnderlineSpan;
import android.util.AttributeSet;
import android.widget.TextView;

/**
 * Created with IntelliJ IDEA.
 * User: Justin
 * Date: 9/11/13
 * Time: 1:10 AM
 */
public class UnderlineTextView extends TextView
{
    private boolean m_modifyingText = false;

    public UnderlineTextView(Context context)
    {
        super(context);
        init();
    }

    public UnderlineTextView(Context context, AttributeSet attrs)
    {
        super(context, attrs);
        init();
    }

    public UnderlineTextView(Context context, AttributeSet attrs, int defStyle)
    {
        super(context, attrs, defStyle);
        init();
    }

    private void init()
    {
        addTextChangedListener(new TextWatcher()
        {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after)
            {
                //Do nothing here... we don't care
            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count)
            {
                //Do nothing here... we don't care
            }

            @Override
            public void afterTextChanged(Editable s)
            {
                if (m_modifyingText)
                    return;

                underlineText();
            }
        });

        underlineText();
    }

    private void underlineText()
    {
        if (m_modifyingText)
            return;

        m_modifyingText = true;

        SpannableString content = new SpannableString(getText());
        content.setSpan(new UnderlineSpan(), 0, content.length(), 0);
        setText(content);

        m_modifyingText = false;
    }
}

现在......只要你想创建XML的下划线TextView的,你只要做到以下几点:

<com.your.package.name.UnderlineTextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_gravity="center_horizontal"
    android:gravity="center"
    android:text="This text is underlined"
    android:textColor="@color/blue_light"
    android:textSize="12sp"
    android:textStyle="italic"/>

我加入这个XML片段的其他选项,以显示我的例子使用改变文字颜色,大小和样式...

希望这有助于!

检查出的下划线可点击按钮式:

<TextView
    android:id="@+id/btn_some_name"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="@string/btn_add_contact"
    android:textAllCaps="false"
    android:textColor="#57a0d4"
    style="@style/Widget.AppCompat.Button.Borderless.Colored" />

<强>的strings.xml:

<string name="btn_add_contact"><u>Add new contact</u></string>

结果:

代替的清洁器的方式,点击 textView.setPaintFlags(textView.getPaintFlags() | Paint.UNDERLINE_TEXT_FLAG); 方法是使用 textView.getPaint().setUnderlineText(true);

和如果需要稍后关闭强调该视图,如在RecyclerView重用图,textView.getPaint().setUnderlineText(false);

只要使用在字符串资源文件e.g的属性。

<string name="example"><u>Example</u></string>

我用这个XML绘制创建底边界和所应用的可拉伸为背景,以我的TextView

<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
    <item>
        <shape android:shape="rectangle" >
            <solid android:color="@android:color/transparent" />
        </shape>
    </item>

    <item android:top="-5dp" android:right="-5dp" android:left="-5dp">
        <shape>
            <solid android:color="@android:color/transparent" />
            <stroke
                    android:width="1.5dp"
                    android:color="@color/pure_white" />
        </shape>
    </item>
</layer-list>

一个简单和灵活的解决方案中的xml:

<View
  android:layout_width="match_parent"
  android:layout_height="3sp"
  android:layout_alignLeft="@+id/your_text_view_need_underline"
  android:layout_alignRight="@+id/your_text_view_need_underline"
  android:layout_below="@+id/your_text_view_need_underline"
  android:background="@color/your_color" />

该死,它是最简单的使用

TextView tv = findViewById(R.id.tv);
tv.setText("some text");

<强>下划线整体的TextView

setUnderLineText(tv, tv.getText().toString());

<强>下划线的TextView的某些部分

setUnderLineText(tv, "some");
  

也支持的TextView儿童的像的EditText,按钮,复选框

public void setUnderLineText(TextView tv, String textToUnderLine) {
        String tvt = tv.getText().toString();
        int ofe = tvt.indexOf(textToUnderLine, 0);

        UnderlineSpan underlineSpan = new UnderlineSpan();
        SpannableString wordToSpan = new SpannableString(tv.getText());
        for (int ofs = 0; ofs < tvt.length() && ofe != -1; ofs = ofe + 1) {
            ofe = tvt.indexOf(textToUnderLine, ofs);
            if (ofe == -1)
                break;
            else {
                wordToSpan.setSpan(underlineSpan, ofe, ofe + textToUnderLine.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
                tv.setText(wordToSpan, TextView.BufferType.SPANNABLE);
            }
        }
    }

检查我的答案在这里,使可点击下划线的文本或TextView的

的下划线多个零件

另一个解决方案是将创建如下所示延伸的TextView定制视图

public class UnderLineTextView extends TextView {

    public UnderLineTextView(Context context) {
        super(context);
        this.setPaintFlags(Paint.UNDERLINE_TEXT_FLAG);
    }

    public UnderLineTextView(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        this.setPaintFlags(Paint.UNDERLINE_TEXT_FLAG);
    }

}

和只是添加到XML如下所示

<yourpackage.UnderLineTextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="underline text"
 />

尝试此代码

在XML

<resource>
 <string name="my_text"><![CDATA[This is an <u>underline</u>]]></string> 
</resources> 

在代码

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

祝您好运!

我简化塞缪尔的的回答是:

<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
    <!--https://stackoverflow.com/a/40706098/4726718-->
    <item
        android:left="-5dp"
        android:right="-5dp"
        android:top="-5dp">
        <shape>
            <stroke
                android:width="1.5dp"
                android:color="@color/colorAccent" />
        </shape>
    </item>
</layer-list>
  1. 转到 strings.xml 资源文件
  2. 必要时在资源文件中添加带有 HTML 下划线标记的字符串。

strings.xml HTML 下划线示例

  1. 在 Java 代码中调用字符串资源 ID,如下所示:
sampleTextView.setText(R.string.sample_string);
  1. 输出中应带有下划线“Stackoverflow”一词。

此外,以下代码不会打印下划线:

String sampleString = getString(R.string.sample_string);
sampleTextView.setText(sampleString);

相反,使用以下代码来保留富文本格式:

CharSequence sampleString = getText(R.string.sample_string);
sampleTextView.setText(sampleString);

“您可以使用 getString(int) 或 getText(int) 来检索字符串。getText(int) 保留应用于字符串的任何富文本样式。” 安卓文档。

参考文档: https://developer.android.com/guide/topics/resources/string-resource.html

我希望这有帮助。

在顶部投答案是正确的和简单的。不过,有时你可能会发现,不工作的一些字体,但对于其他人的工作。(这问题,我刚与中国打交道时,碰上了。)

解决方法是不使用只为你的TextView“WRAP_CONTENT”,原因有绘制的线条没有多余的空间。你可以设置固定的高度,以您的TextView,或使用Android:paddingVertical与WRAP_CONTENT

HtmlCompat.fromHtml(
                    String.format(context.getString(R.string.set_target_with_underline)),
                    HtmlCompat.FROM_HTML_MODE_LEGACY)
<string name="set_target_with_underline">&lt;u>Set Target&lt;u> </string>

请注意在xml文件退出符号

我在那里我使用自定义的字体和与资源文件绝招(<u>Underlined text</u>)创建的下划线没工作中的问题,但Android的管理,以下划线转化为一种打击低谷。

我用这个答案绘制TextView的下方边框自己: https://stackoverflow.com/a/10732993/664449 。显然,这并不用于部分加下划线的文本或文本multilined工作。

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top