如何将字体样式设置为Android TextView中的粗体,斜体和下划线?
-
30-09-2019 - |
题
我想做一个 TextView
内容大胆,斜体和下划线。我尝试了以下代码,并且可以使用,但不强调。
<Textview android:textStyle="bold|italic" ..
我该怎么做?有快速的想法吗?
解决方案
我不知道下划线,但是对于大胆而斜体 "bolditalic"
. 。这里没有提及下划线: http://developer.android.com/reference/android/widget/textview.html#attr_android:textstyle
介意您使用上述 bolditalic
您需要,我从该页面引用
必须是以下常数值的一个或多个(被'|'隔开)。
所以你会使用 bold|italic
您可以检查此问题是否下划线: 我可以在Android布局中强调文本吗?
其他提示
这应该使您的文本视图 胆大, 下划线 和 斜体 同时。
strings.xml
<resources>
<string name="register"><u><b><i>Copyright</i></b></u></string>
</resources>
要将此字符串设置为您的文本视图,请在您的 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" />
或IN 爪哇,
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);
输出
或像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);
保持简单和一行:)
对于大胆和斜体
hellyandroid.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);
}
}
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/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>
这是一种添加下划线的简便方法,同时维护其他设置:
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"
没有报价对我有用:
<item name="android:textStyle">bold|italic</item>
如果您是从文件或网络读取该文本的。
您可以通过在文本中添加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));
style="?android:attr/listSeparatorTextViewStyle
- 通过制作这种风格,您可以实现强调
不隶属于 StackOverflow