我想让TextView
的内容变得粗体,斜体和下划线。 我尝试了如下代码而且它能够工做,但没有强调。 android
<Textview android:textStyle="bold|italic" ..
我该怎么作? 任何快速的想法? 编程
这是添加下划线的简单方法,同时保持其余设置: 网络
textView.setPaintFlags(textView.getPaintFlags() | Paint.UNDERLINE_TEXT_FLAG);
或者像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中: spa
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);
保持简单,一行:) code
style="?android:attr/listSeparatorTextViewStyle
若是您正在从文件或网络中读取该文本。 orm
您能够经过向所提到的文本添加HTML标记来实现它 字符串
This text is <i>italic</i> and <b>bold</b> and <u>underlined</u> <b><i><u>bolditalicunderlined</u></b></i>
而后,您能够使用将HTML字符串处理为可显示样式文本的HTML类。 get
// textString is the String after you retrieve it from the file textView.setText(Html.fromHtml(textString));
Programmatialy: 字符串处理
您能够使用setTypeface()方法以编程方式执行:
下面是默认字体的代码
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
XML:
您能够直接在XML文件中设置:
android:textStyle="normal" android:textStyle="normal|bold" android:textStyle="normal|italic" android:textStyle="bold" android:textStyle="bold|italic"