简体   繁体   English

如何在 Android TextView 中将字体样式设置为粗体、斜体和下划线?

[英]How to set the font style to bold, italic and underlined in an Android TextView?

I want to make a TextView 's content bold, italic and underlined.我想让TextView的内容加粗、斜体和带下划线。 I tried the following code and it works, but doesn't underline.我尝试了以下代码并且它有效,但没有下划线。

<Textview android:textStyle="bold|italic" ..

How do I do it?我该怎么做? Any quick ideas?任何快速的想法?

This should make your TextView bold , underlined and italic at the same time.这应该使您的 TextView 同时加粗加下划线斜体

strings.xml字符串.xml

<resources>
    <string name="register"><u><b><i>Copyright</i></b></u></string>
</resources>

To set this String to your TextView, do this in your main.xml将此字符串设置为您的 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" />

or In JAVA ,或在JAVA 中

TextView textView = new TextView(this);
textView.setText(R.string.register);

Sometimes the above approach will not be helpful when you might have to use Dynamic Text.有时,当您可能不得不使用动态文本时,上述方法将无济于事。 So in that case SpannableString comes into action.所以在这种情况下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);

OUTPUT输出

在此处输入图片说明

I don't know about underline, but for bold and italic there is "bolditalic" .我不知道下划线,但对于粗体和斜体有"bolditalic" There is no mention of underline here: http://developer.android.com/reference/android/widget/TextView.html#attr_android:textStyle这里没有提到下划线: http : //developer.android.com/reference/android/widget/TextView.html#attr_android : textStyle

Mind you that to use the mentioned bolditalic you need to, and I quote from that page请注意,要使用提到的bolditalic您需bolditalic ,我引用了该页面

Must be one or more (separated by '|') of the following constant values.必须是以下常量值中的一个或多个(以“|”分隔)。

so you'd use bold|italic所以你会使用bold|italic

You could check this question for underline: Can I underline text in an android layout?你可以检查这个问题的下划线: Can I underline text in an android layout?

Or just like this in Kotlin:或者就像在 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

Or in Java:或者在 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);

Keep it simple and in one line :)保持简单并在一行中:)

For bold and italic whatever you are doing is correct for underscore use following code对于粗体和斜体,无论你在做什么都是正确的下划线使用以下代码

HelloAndroid.java你好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);
}
}

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

<?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);

Programmatialy:以编程方式:

You can do programmatically using setTypeface() method:您可以使用 setTypeface() 方法以编程方式执行:

Below is the code for default 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

and if you want to set custom Typeface:如果你想设置自定义字体:

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:

You can set Directly in XML file in like:您可以直接在 XML 文件中设置,例如:

android:textStyle="normal"
android:textStyle="normal|bold"
android:textStyle="normal|italic"
android:textStyle="bold"
android:textStyle="bold|italic"

If you are reading that text from a file or from the network.如果您正在从文件或网络中读取该文本。

You can achieve it by adding HTML tags to your text like mentioned您可以通过向您的文本添加 HTML 标签来实现它

This text is <i>italic</i> and <b>bold</b>
and <u>underlined</u> <b><i><u>bolditalicunderlined</u></b></i>

and then you can use the HTML class that processes HTML strings into displayable styled text.然后您可以使用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>

xml中只有一行代码

        android:textStyle="italic"

You can achieve it easily by using Kotlin's buildSpannedString{} under its core-ktx dependency.您可以通过在其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
    style="?android:attr/listSeparatorTextViewStyle
  • 通过制作这种样式,您可以实现下划线

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM