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

d-m*_*man 419 android text-styling textview

我想把TextView内容大胆,斜体和下划线.我尝试了以下代码并且它可以工作,但没有强调.

<Textview android:textStyle="bold|italic" ..
Run Code Online (Sandbox Code Playgroud)

我该怎么做?任何快速的想法?

And*_*lva 353

这应该使您的TextView同时变为粗体,下划线斜体.

strings.xml中

<resources>
    <string name="register"><u><b><i>Copyright</i></b></u></string>
</resources>
Run Code Online (Sandbox Code Playgroud)

要将此String设置为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" />
Run Code Online (Sandbox Code Playgroud)

或者在JAVA中,

TextView textView = new TextView(this);
textView.setText(R.string.register);
Run Code Online (Sandbox Code Playgroud)

有时,当您可能必须使用动态文本时,上述方法将没有用处.因此,在这种情况下,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);
Run Code Online (Sandbox Code Playgroud)

OUTPUT

在此输入图像描述

  • 我在2.1中检查过它.所以至少它应该从2.1及以上工作 (3认同)

Nan*_*nne 264

我不知道下划线,但粗体和斜体有"bolditalic".这里没有提到下划线:http://developer.android.com/reference/android/widget/TextView.html#attr_android : textStyle

请注意,使用bolditalic您需要的提及,我引用该页面

必须是以下常量值中的一个或多个(由"|"分隔).

所以你要用 bold|italic

您可以检查下划线的问题:我可以在Android布局中为文本加下划线吗?

  • for under line ..`textView.setPaintFlags(Paint.UNDERLINE_TEXT_FLAG);` (45认同)
  • @bala 请注意,您的解决方案总是在整个文本下划线,因此如果您只想在其中的一部分下划线,这是不可行的。 (2认同)

h0u*_*sni 139

或者像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
Run Code Online (Sandbox Code Playgroud)

或者在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);
Run Code Online (Sandbox Code Playgroud)

保持简单,一行:)

  • 通过为您正在使用的视图插入 kotlinx.android.synthetic 包,在 Kotlin 中不需要 findViewByID,从而使每个 setTypeface 行: textViewOne.setTypeface(...) (2认同)

Viv*_*vek 74

对于粗体和斜体,无论你做什么都是正确的下划线使用下面的代码

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);
}
}
Run Code Online (Sandbox Code Playgroud)

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"/>
Run Code Online (Sandbox Code Playgroud)

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>
Run Code Online (Sandbox Code Playgroud)


son*_*ida 46

这是添加下划线的简单方法,同时保持其他设置:

textView.setPaintFlags(textView.getPaintFlags() | Paint.UNDERLINE_TEXT_FLAG);
Run Code Online (Sandbox Code Playgroud)


Kin*_*ses 36

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
Run Code Online (Sandbox Code Playgroud)

如果要设置自定义字体:

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
Run Code Online (Sandbox Code Playgroud)

XML:

您可以直接在XML文件中设置:

android:textStyle="normal"
android:textStyle="normal|bold"
android:textStyle="normal|italic"
android:textStyle="bold"
android:textStyle="bold|italic"
Run Code Online (Sandbox Code Playgroud)


Ahm*_*azy 21

如果您正在从文件或网络中读取该文本.

您可以通过向所提到的文本添加HTML标记来实现它

This text is <i>italic</i> and <b>bold</b>
and <u>underlined</u> <b><i><u>bolditalicunderlined</u></b></i>
Run Code Online (Sandbox Code Playgroud)

然后,您可以使用将HTML字符串处理为可显示样式文本的HTML类.

// textString is the String after you retrieve it from the file
textView.setText(Html.fromHtml(textString));
Run Code Online (Sandbox Code Playgroud)


小智 20

没有引号适合我:

<item name="android:textStyle">bold|italic</item>
Run Code Online (Sandbox Code Playgroud)


Bor*_*nov 7

xml中只有一行代码

        android:textStyle="italic"
Run Code Online (Sandbox Code Playgroud)


Mor*_*Koh 6

您可以通过buildSpannedString{}在其core-ktx依赖项下使用 Kotlin 来轻松实现它。

val formattedString = buildSpannedString {
    append("Regular")
    bold { append("Bold") }
    italic { append("Italic") }
    underline { append("Underline") }
    bold { italic {append("Bold Italic")} }
}

textView.text = formattedString
Run Code Online (Sandbox Code Playgroud)


dre*_*per 5

    style="?android:attr/listSeparatorTextViewStyle
Run Code Online (Sandbox Code Playgroud)
  • 通过制作这种风格,你可以实现强调