我想要强调 TextView 's content bold, italic and underlined. I tried the following code and it works, but doesn' t .
<Textview android:textStyle="bold|italic" ..
我该怎么做?任何快速的想法?
9 回答
125
或者像Kotlin一样:
val tv = findViewById(R.id.textViewOne) as TextView
tv.setTypeface(null, Typeface.BOLD_ITALIC)
// OR
tv.setTypeface(null, Typeface.BOLD or Typeface.ITALIC)
// OR
tv.setTypeface(null, Typeface.BOLD)
// OR
tv.setTypeface(null, Typeface.ITALIC)
// AND
tv.paintFlags = tv.paintFlags or Paint.UNDERLINE_TEXT_FLAG
或者在Java中:
TextView tv = (TextView)findViewById(R.id.textViewOne);
tv.setTypeface(null, Typeface.BOLD_ITALIC);
// OR
tv.setTypeface(null, Typeface.BOLD|Typeface.ITALIC);
// OR
tv.setTypeface(null, Typeface.BOLD);
// OR
tv.setTypeface(null, Typeface.ITALIC);
// AND
tv.setPaintFlags(tv.getPaintFlags()|Paint.UNDERLINE_TEXT_FLAG);
textView.setTypeface(null, Typeface.NORMAL); // for Normal Text
textView.setTypeface(null, Typeface.BOLD); // for Bold only
textView.setTypeface(null, Typeface.ITALIC); // for Italic
textView.setTypeface(null, Typeface.BOLD_ITALIC); // for Bold and Italic
如果要设置自定义字体:
textView.setTypeface(textView.getTypeface(), Typeface.NORMAL); // for Normal Text
textView.setTypeface(textView.getTypeface(), Typeface.BOLD); // for Bold only
textView.setTypeface(textView.getTypeface(), Typeface.ITALIC); // for Italic
textView.setTypeface(textView.getTypeface(), Typeface.BOLD_ITALIC); // for Bold and Italic
9 回答
或者像Kotlin一样:
或者在Java中:
保持简单,一行:)
我不知道下划线,但是对于粗体和斜体,有
"bolditalic"
. 这里没有提到下划线:http://developer.android.com/reference/android/widget/TextView.html#attr_android:textStyle请注意,使用你需要提到的
bolditalic
,我引用该页面所以你要用
bold|italic
你可以查看这个问题的下划线:Can I underline text in an android layout?
这应该使你的TextView bold , underlined 和 italic 同时 .
strings.xml
要将此String设置为TextView,请在 main.xml 中执行此操作
或在 JAVA ,
有时,当您可能必须使用动态文本时,上述方法将没有用处 . 所以在这种情况下SpannableString开始行动了 .
OUTPUT
对于粗体和斜体,无论你做什么都是正确的下划线使用下面的代码
HelloAndroid.java
main.xml中
string.xml
这是添加下划线的简单方法,同时保持其他设置:
Programmatialy:
您可以使用setTypeface()方法以编程方式执行:
下面是默认字体的代码
如果要设置自定义字体:
XML:
您可以直接在XML文件中设置:
没有引号适合我:
If you are reading that text from a file or from the network.
您可以通过向所提到的文本添加HTML标记来实现它
然后您可以使用HTML类将HTML字符串处理为可显示的样式文本 .