我想将 TextView
的内容设为粗体、斜体和下划线。我尝试了以下代码,它可以工作,但没有下划线。
<Textview android:textStyle="bold|italic" ..
我该怎么做?有什么快速的想法吗?
tv.setTypeface(null, Typeface.BOLD_ITALIC);
这应该同时使您的 TextView 粗体、下划线和斜体。
字符串.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);
输出
https://i.stack.imgur.com/07SZg.png
我不知道下划线,但粗体和斜体有 "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);
或者就像在 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
有必要吗?没有它就可以工作
对于粗体和斜体,您所做的任何事情对于下划线都是正确的,请使用以下代码
你好Android.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"/>
字符串.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="hello">Hello World, HelloAndroid!</string>
<string name="app_name">Hello, Android</string>
</resources>
underline
传递 Null 值而不是 new UnderlineSpan()
,如下 content.setSpan(null, 0, content.length(), 0);
这是添加下划线的简单方法,同时保留其他设置:
textView.setPaintFlags(textView.getPaintFlags() | Paint.UNDERLINE_TEXT_FLAG);
程序化:
您可以使用 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"
如果您正在从文件或网络中读取该文本。
您可以通过在您的文本中添加 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>
您可以通过在其 core-ktx
依赖项下使用 Kotlin 的 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"
style="?android:attr/listSeparatorTextViewStyle
通过制作这种风格,你可以实现下划线
new StyleSpan(Typeface.BOLD_ITALIC)