ChatGPT解决这个技术问题 Extra ChatGPT

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

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

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

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

只设置其中一个是否有效?
是的,工作正常,我也想把它放在下面。
textView.setPaintFlags(Paint.UNDERLINE_TEXT_FLAG);
tv.setTypeface(null, Typeface.BOLD_ITALIC);

1
12 revs, 2 users 86%

这应该同时使您的 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


我在 2.1 中检查了它。所以至少它应该从 2.1 及更高版本开始工作
可以考虑使用new StyleSpan(Typeface.BOLD_ITALIC)
为什么它不适用于动态连接字符串?出现了一些数字是有线的....
C
Community

我不知道下划线,但粗体和斜体有 "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);
@bala 请注意,您的解决方案始终会在整个文本下划线,因此如果您只想在其中的一部分下划线,这是不可行的。
如果可能,最好使用 xml 而不是通过代码来完成。请参阅下面的 Andro Selva 的回答
佚名

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

保持简单和一行:)


通过为您正在使用的视图插入 kotlinx.android.synthetic 包,在 Kotlin 中不需要 findViewByID,从而使每一行 setTypeface 成为: textViewOne.setTypeface(...)
paintFlags 有必要吗?没有它就可以工作
m
msrd0

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

你好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);
P
Phil

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

textView.setPaintFlags(textView.getPaintFlags() | Paint.UNDERLINE_TEXT_FLAG);

请注意,此解决方案始终会为整个文本添加下划线,因此在您只想在其中的一部分下划线的情况下是不可行的。
K
King of Masses

程序化:

您可以使用 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"

如何使用 setTypeface 更改字体系列也应用粗体、斜体、下划线?
A
Ahmed Hegazy

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

您可以通过在您的文本中添加 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));

fromHtml(String) 方法在 API 级别 24 中已弃用此处更多讨论:stackoverflow.com/questions/37904739/…
L
Lotfi

没有引号对我有用:

<item name="android:textStyle">bold|italic</item>

M
Morgan Koh

您可以通过在其 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

B
Bo Z

xml中只有一行代码

        android:textStyle="italic"

d
dreamdeveloper
    style="?android:attr/listSeparatorTextViewStyle

通过制作这种风格,你可以实现下划线