如何在Android布局xml文件中定义带下划线的文本?
当前回答
对于Button和TextView,这是最简单的方法:
按钮:
Button button = (Button) findViewById(R.id.btton1);
button.setPaintFlags(button.getPaintFlags() | Paint.UNDERLINE_TEXT_FLAG);
文本视图:
TextView textView = (TextView) findViewById(R.id.textview1);
textView.setPaintFlags(textView.getPaintFlags() | Paint.UNDERLINE_TEXT_FLAG);
其他回答
如果您使用的是字符串资源xml文件,该文件支持HTML标记,如<b></b>、<i></i>和<u></u>,则可以实现这一点。
<resources>
<string name="your_string_here"><![CDATA[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
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" />
字符串.xml:
<string name="btn_add_contact"><u>Add new contact</u></string>
结果:
我知道这是一个迟来的答案,但我想出了一个非常有效的解决方案。。。我从Anthony 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中创建带下划线的文本视图时,只需执行以下操作:
<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片段中添加了其他选项,以显示我的示例可以更改文本颜色、大小和样式。。。
希望这有帮助!
转到strings.xml资源文件必要时,在资源文件中添加带有HTML下划线标记的字符串。
strings.xml HTML下划线示例
按如下方式调用Java代码中的字符串资源ID:
sampleTextView.setText(R.string.sample_string);
输出应带有下划线的单词“Stacksoverflow”。
此外,以下代码不会打印下划线:
String sampleString = getString(R.string.sample_string);
sampleTextView.setText(sampleString);
相反,使用以下代码保留RTF格式:
CharSequence sampleString = getText(R.string.sample_string);
sampleTextView.setText(sampleString);
“您可以使用getString(int)或getText(int)来检索字符串。getText(int)保留应用于字符串的任何富文本样式。”Android文档。
请参阅文档:https://developer.android.com/guide/topics/resources/string-resource.html
我希望这有帮助。
在Kotlin中可以使用扩展函数。这只能从代码中使用,而不能从xml中使用。
fun TextView.underline() {
paintFlags = paintFlags or Paint.UNDERLINE_TEXT_FLAG
}
用法:
tv_change_number.underline()
tv_resend_otp.underline()
推荐文章
- 我如何在一个片段中访问getSupportFragmentManager() ?
- 调试在哪里。Android Studio中的密钥存储库
- 从资产中读取文件
- 在不活动的地方调用getLayoutInflater()
- 设置Android布局元素的背景颜色
- 错误:'keytool'不能被识别为内部或外部命令、可操作程序或批处理文件
- 在应用程序本身中更改Locale
- apk (.apk)和应用程序包(.aab)的区别
- 如何设置超时在改造库?
- Android - SPAN_EXCLUSIVE_EXCLUSIVE跨度不能为零长度
- TextView的字体大小在Android应用程序改变字体大小从本机设置
- 如何模拟Android杀死我的进程
- 禁用EditText闪烁光标
- Android Eclipse -无法找到*.apk
- 设置TextView文本从html格式的字符串资源在XML