【问题标题】:How to set the font style to bold, italic and underlined in an Android TextView?如何在 Android TextView 中将字体样式设置为粗体、斜体和下划线?
【发布时间】:2023-03-29 02:45:02
【问题描述】:

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

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

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

【问题讨论】:

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

标签: android textview text-styling


【解决方案1】:

xml中只有一行代码

        android:textStyle="italic"

【讨论】:

    【解决方案2】:

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

    【讨论】:

      【解决方案3】:

      或者就像在 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 有必要吗?没有它就可以工作
      【解决方案4】:

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

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

      【讨论】:

      【解决方案5】:

      程序化:

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

      【讨论】:

      【解决方案6】:

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

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

      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"/>
      

      字符串.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传递空值而不是new UnderlineSpan(),如下content.setSpan(null, 0, content.length(), 0);
      【解决方案7】:
          style="?android:attr/listSeparatorTextViewStyle
      
      • 通过制作这种样式,你可以实现下划线

      【讨论】:

        【解决方案8】:

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

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

        【讨论】:

        • 请注意,此解决方案始终会为整个文本添加下划线,因此如果只想在其中的一部分下划线,则不可行。
        【解决方案9】:

        没有引号对我有用:

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

        【讨论】:

          【解决方案10】:

          这应该使您的 TextView 同时粗体下划线斜体

          strings.xml

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

          要将此字符串设置为您的 TextView,请在您的 ma​​in.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);
          

          输出

          【讨论】:

          • 我在 2.1 中检查过。所以至少它应该从 2.1 及更高版本开始工作
          • 可以考虑使用new StyleSpan(Typeface.BOLD_ITALIC)
          • 为什么它不适用于动态连接字符串?一些数字出现了……
          【解决方案11】:

          我不知道下划线,但粗体和斜体有"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 的答案
          猜你喜欢
          • 2011-03-03
          • 1970-01-01
          • 1970-01-01
          • 2020-07-26
          • 2012-07-20
          • 2010-11-25
          • 1970-01-01
          • 1970-01-01
          • 2013-11-06
          相关资源
          最近更新 更多