TextView
のコンテンツを太字、斜体、下線付きにしたい。次のコードを試してみましたが、動作しますが、下線が引かれません。
<Textview android:textStyle="bold|italic" ..
どうすればいいのですか?簡単なアイデアはありますか?
TextView
のコンテンツを太字、斜体、下線付きにしたい。次のコードを試してみましたが、動作しますが、下線が引かれません。
<Textview android:textStyle="bold|italic" ..
どうすればいいのですか?簡単なアイデアはありますか?
これにより、 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);
出力
下線についてはわかりませんが、太字と斜体には"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?
または、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行にまとめてください:)
太字と斜体の場合、何をしていても、アンダースコアは次のコードで使用できます
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>
これは、他の設定を維持しながら下線を追加する簡単な方法です。
textView.setPaintFlags(textView.getPaintFlags() | Paint.UNDERLINE_TEXT_FLAG);
プログラムで:
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"
そのテキストをファイルまたはネットワークから読んでいる場合。
上記のように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));
引用符なしでうまくいきます:
<item name="android:textStyle">bold|italic</item>
style="?android:attr/listSeparatorTextViewStyle