508

我想让 aTextView的内容加粗、斜体和下划线。我尝试了以下代码,它可以工作,但没有下划线。

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

我该怎么做?有什么快速的想法吗?

4

11 回答 11

384

这应该使您的 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);

输出

在此处输入图像描述

于 2011-03-02T15:22:48.400 回答
300

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

请注意,要使用bolditalic您需要使用的内容,我从该页面引用

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

所以你会使用bold|italic

你可以检查这个问题是否有下划线:我可以在 android 布局中给文本加下划线吗?

于 2011-01-07T07:59:43.150 回答
173

或者就像在 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);

保持简单和一行:)

于 2013-04-01T12:35:08.470 回答
81

对于粗体和斜体,您所做的任何事情对于下划线都是正确的,请使用以下代码

你好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>
于 2011-01-07T08:59:24.697 回答
50

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

textView.setPaintFlags(textView.getPaintFlags() | Paint.UNDERLINE_TEXT_FLAG);
于 2013-01-03T04:29:15.997 回答
43

程序化:

您可以使用 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"
于 2015-06-08T12:31:28.447 回答
25

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

您可以通过在您的文本中添加 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));
于 2015-01-08T23:07:29.830 回答
21

没有引号对我有用:

<item name="android:textStyle">bold|italic</item>
于 2011-08-11T09:07:21.297 回答
9

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
于 2020-02-26T09:22:46.823 回答
9

xml中只有一行代码

        android:textStyle="italic"
于 2020-03-20T18:08:33.777 回答
5
    style="?android:attr/listSeparatorTextViewStyle
  • 通过制作这种风格,你可以实现下划线
于 2014-09-02T09:26:26.687 回答