508

TextViewのコンテンツを太字、斜体、下線付きにしたい。次のコードを試してみましたが、動作しますが、下線が引かれません。

<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必要があることに注意してください。そのページから引用します

次の定数値の 1 つ以上 ('|' で区切られた) である必要があります。

だからあなたは使うだろうbold|italic

下線についてこの質問を確認できます: Can I underline text in an android layout?

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

シンプルに1行にまとめてください:)

于 2013-04-01T12:35:08.470 に答える
81

太字と斜体の場合、何をしていても、アンダースコアは次のコードで使用できます

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>
于 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 に答える
5
    style="?android:attr/listSeparatorTextViewStyle
  • このスタイルを作ることで、下線を引くことができます
于 2014-09-02T09:26:26.687 に答える