Android TextViewでフォントスタイルを大胆で斜体、下線に設定するにはどうすればよいですか?
-
30-09-2019 - |
質問
作りたい TextView
のコンテンツは大胆で、イタリック体で、下線が引かれています。次のコードを試しましたが、機能しますが、下線はありません。
<Textview android:textStyle="bold|italic" ..
どうすればいいのですか?簡単なアイデアはありますか?
解決
私は下線については知りませんが、大胆でイタリック体にはあります "bolditalic"
. 。ここに下線の言及はありません: http://developer.android.com/reference/android/widget/textview.html#attr_android:textstyle
言及されたものを使用することに注意してください bolditalic
あなたがする必要があり、私はそのページから引用します
次の定数値の1つ以上( '|'で区切られている)でなければなりません。
だからあなたは使用するでしょう bold|italic
この質問は、下線を確認できます。 Androidレイアウトでテキストを下に下回ることはできますか?
他のヒント
これにより、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" />
またはで ジャワ,
TextView textView = new TextView(this);
textView.setText(R.string.register);
動的テキストを使用する必要がある場合に、上記のアプローチが役に立たない場合があります。その場合、その場合 スパナブルストリング 行動を起こします。
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);
出力
または、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"/>
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>
これは、他の設定を維持しながら、下線を追加する簡単な方法です。
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"
引用がないことは私のために働いています:
<item name="android:textStyle">bold|italic</item>
ファイルまたはネットワークからそのテキストを読んでいる場合。
言及されたようにテキストに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));
style="?android:attr/listSeparatorTextViewStyle
- このスタイルを作ることで、Uは下線を導くことができます