我想让一个 TextView
的内容加粗、斜体和带下划线。我尝试了以下代码,它可以实现加粗和斜体,但无法实现下划线。
<Textview android:textStyle="bold|italic" ..
我该如何做?有什么快速的想法吗?
我想让一个 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);
输出
new StyleSpan(Typeface.BOLD_ITALIC)
。 - Cheok Yan Cheng我对下划线不确定,但是对于加粗和斜体,可以使用"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.setPaintFlags(Paint.UNDERLINE_TEXT_FLAG);
- bCliks或者在 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);
保持简单,一行搞定 :)
paintFlags
是必要的吗?没有它也能工作。 - Prabs对于粗体和斜体,您正在进行的操作是正确的。对于下划线,请使用以下代码:
HelloAndroid.java
package com.example.helloandroid;
import android.app.Activity;
import android.os.Bundle;
import android.text.SpannableString;
import android.text.style.UnderlineSpan;
import android.widget.TextView;
public class HelloAndroid extends Activity {
TextView textview;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
textview = (TextView)findViewById(R.id.textview);
SpannableString content = new SpannableString(getText(R.string.hello));
content.setSpan(new UnderlineSpan(), 0, content.length(), 0);
textview.setText(content);
}
}
主要.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/hello"
android:textStyle="bold|italic"/>
string.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="hello">Hello World, HelloAndroid!</string>
<string name="app_name">Hello, Android</string>
</resources>
setSpan
方法中的参数,而不是传递 new UnderlineSpan()
,如下所示:content.setSpan(null, 0, content.length(), 0);
- Sami Eltamawy这是一种简单的方法来添加下划线,同时保持其他设置:
textView.setPaintFlags(textView.getPaintFlags() | Paint.UNDERLINE_TEXT_FLAG);
可以通过setTypeface()方法进行编程实现:
以下是默认Typeface的代码:
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"
如果您从文件或网络中读取该文本。
您可以通过添加HTML标签来实现,就像所提到的那样。
This text is <i>italic</i> and <b>bold</b>
and <u>underlined</u> <b><i><u>bolditalicunderlined</u></b></i>
接着您可以使用HTML类,将HTML字符串处理为可显示的样式文本。
// textString is the String after you retrieve it from the file
textView.setText(Html.fromHtml(textString));
没有引号对我有效:
<item name="android:textStyle">bold|italic</item>
通过使用 Kotlin 的 core-ktx
依赖项下的 buildSpannedString{}
,您可以轻松实现它。
val formattedString = buildSpannedString {
append("Regular")
bold { append("Bold") }
italic { append("Italic") }
underline { append("Underline") }
bold { italic {append("Bold Italic")} }
}
textView.text = formattedString
只需一行xml代码
android:textStyle="italic"