我想让 TextView
的内容变为粗体、斜体和下划线。我尝试了以下代码,它可以工作,但没有下划线。
<Textview android:textStyle="bold|italic" ..
我该怎么做?有什么快速的想法吗?
最佳答案
这应该使您的 TextView 同时粗体、下划线和斜体。
strings.xml
<resources>
<string name="register"><u><b><i>Copyright</i></b></u></string>
</resources>
要将此字符串设置为您的 TextView,请在您的 main.xml 中执行此操作
<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/textview"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:text="@string/register" />
或在JAVA中,
TextView textView = new TextView(this);
textView.setText(R.string.register);
有时,当您可能不得不使用动态文本时,上述方法将无济于事。所以在那种情况下SpannableString开始行动。
String tempString="Copyright";
TextView text=(TextView)findViewById(R.id.text);
SpannableString spanString = new SpannableString(tempString);
spanString.setSpan(new UnderlineSpan(), 0, spanString.length(), 0);
spanString.setSpan(new StyleSpan(Typeface.BOLD), 0, spanString.length(), 0);
spanString.setSpan(new StyleSpan(Typeface.ITALIC), 0, spanString.length(), 0);
text.setText(spanString);
输出
关于android - 如何在 Android TextView 中将字体样式设置为粗体、斜体和下划线?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4623508/