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

我想使TextView的内容粗体,斜体和下划线。 我试了下面的代码,它的工作原理,但不强调。

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

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

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

请注意,您需要使用所提到的bolditalic内容,并从该页面引用

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

所以你会使用bold|italic

你可以检查这个问题的下划线: 我可以在android布局中下划线文本?

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

strings.xml中

  Copyright  

要将此字符串设置为您的TextView,请在main.xml中执行此操作

   

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

OUTPUT

在这里输入图像说明

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

保持简单,并在一行:)

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

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中

   

string.xml

   Hello World, HelloAndroid! Hello, Android  

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

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

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 

如果你想设置自定义字体:

 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" 

没有引号对我有用:

 bold|italic 

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

你可以通过添加HTML标签到你的文字,如上所述

 This text is italic and bold and underlined bolditalicunderlined 

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

 // textString is the String after you retrieve it from the file textView.setText(Html.fromHtml(textString)); 
  style="?android:attr/listSeparatorTextViewStyle 
  • 通过这种风格,你可以实现下划线