Android TextViewのよくつかう色々まとめ
AndoridのTextViewよく使いますねー。
毎回調べるのも大変なので、ブログに書いておきますー
確か一番最初に出会ったViewが、
HelloWorldのTextViewだった記憶がありますw
TextViewのいろいろ
- テキスト設定
- XML
- プログラム
- HTML
- テキストサイズ
- テキストスタイル(太字・斜体)
- 独自フォント設定
- テキストカラー
- その他
環境
AndroidStudio 3
Java
DataBinding
テキスト設定
XML
<!--android:text="ここに静的テキストを!"--> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Hello World!" />
<!--android:text="@string/***で設定!"--> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/app_name" />
固定文言は string.xml に記載したほうが英語等複数言語対応する際には良い!
プログラム
// 動的テキスト設定! mBinding.text1.setText("動的HelloWorld!!"); // strings.xmlのリソースも指定できる! mBinding.text2.setText(R.string.app_name);
動的にテキストを切り替える時によく使う
HTML
//HTML指定 String html ="普通のテキスト<B>太字</B><BIG>大きく</BIG><font color=\"red\">赤</font>"; mBinding.text3.setText(Html.fromHtml(html));
うまく使えば一つのTextViewで対応可能!
ただ、文字サイズとか細かく調整できないので細かいデザイン調整などには不向きかも。。。
あと更新に弱いので、ListView等の表示では複数TextViewを使って対応したほうが良い!
テキストサイズ
<!--textSize="**sp"で指定--> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="20spだよ!" android:textSize="20sp" />
// テキストサイズ指定 mBinding.text4.setTextSize(20);
SPという単位の詳細は下記サイトにまとめられています!
Y.A.M の 雑記帳
y-anz-m.blogspot.jp
テキストスタイル(太字・斜体)
<!--textStyle="***"で設定--> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="テキストスタイル" android:textStyle="bold|italic"/>
「|」を使って太字と斜体を組み合わせている。
もちろん単一設定もOK!
// 太字
mBinding.text5.setTypeface(Typeface.DEFAULT_BOLD)
独自フォント設定
// 独自フォントをAssetsディレクトリから取得 Typeface typeface = Typeface.createFromAsset(getAssets(), "APJapanesefont.ttf"); mBinding.text6.setTypeface(typeface);
あんずもじフォントを利用させていただきました!
フォント名:あんずもじ
作成者:京風子(Kyoko)
配布サイト名:あんずいろapricot×color
URL:http://www8.plala.or.jp/p_dolce/
www8.plala.or.jp
テキストカラー
<!--colors.xmlリソースから設定--> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="色!" android:textColor="@color/colorPrimary"/> <!--直接指定 #RGB 3桁--> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="色!" android:textColor="#f00"/> <!--直接指定 #RGB 6桁--> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="色!" android:textColor="#ff0000"/> <!--直接指定 #ARGB 8桁 Aはアルファ値--> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="色!" android:textColor="#55ff0000"/>
// ARGBで指定 mBinding.text7.setTextColor(0xffff0000); // colors.xmlリソースから色設定 少し長くなる癖がある! mBinding.text8.setTextColor(ContextCompat.getColor(this, R.color.colorPrimaryDark)); // Colorクラスを使った設定方法 mBinding.text9.setTextColor(Color.parseColor("#00ff00"));
// AppendでTextViewの文字を連結する事ができる for(int i = 0; i < 3; i++){ mBinding.text10.append("Appendで追加!"); mBinding.text10.append(String.valueOf(i)); mBinding.text10.append("\n"); }
全部やるとこんな感じ!w
Github
github.com
あげてますー
その他
調べている最中に、TextViewCompatなるものも知りました。
最大文字サイズ設定など、使いやすそうです。
暇な時に調べて書きますw