2011-01-07 251 views
334

我想使TextView的內容粗體,斜體和下劃線。我嘗試了下面的代碼,它的工作原理,但不強調。如何在Android TextView中將字體樣式設置爲粗體,斜體和下劃線?

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

我該怎麼做?任何快速的想法?

+0

它的工作原理設置只是其中之一? – falstro 2011-01-07 07:47:40

+0

是的工作正常我也想讓它下線。 – 2011-01-07 07:48:55

+5

textView.setPaintFlags(Paint.UNDERLINE_TEXT_FLAG); – bCliks 2013-09-11 05:50:59

回答

211

我不知道下劃線,但大膽和斜體有"bolditalic"。沒有下劃線的在這裏提一下:http://developer.android.com/reference/android/widget/TextView.html#attr_android:textStyle

提個醒,要使用所提到的bolditalic你需要,我從該頁面

引號必須是一個或多個(分隔「|」)的以下常數值。

所以你最好使用bold|italic

你可以檢查這個問題的底線:Can I underline text in an android layout?

+43

for under .. `textView.setPaintFlags(Paint.UNDERLINE_TEXT_FLAG);` – bCliks 2013-09-11 05:51:51

+1

@bala請注意,您的解決方案始終強調整個文本,所以它不可行如果只想強調一部分的話。 – 2014-04-02 18:14:35

62

對於粗體和斜體無論你正在做的是下面的代碼

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

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> 
+0

要刪除`underline`傳遞Null值而不是`new UnderlineSpan()`,如下所示:`content.setSpan(null,0,content.length(),0);` – 2015-05-17 08:54:53

332

這應該讓你的TextView 大膽同時強調斜體

的strings.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); 

輸出

enter image description here

18

無報價爲我工作:

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

這是加下劃線的簡單方法,同時保持其他設置:

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

或者只是像這樣在科特林:

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

保持簡單,在同一行:)

4
style="?android:attr/listSeparatorTextViewStyle 
  • 通過使這種風格,你可以實現下劃線
16

如果您正在從文件或網絡中讀取該文本。

您可以將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)); 
29

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" 
相關問題