テキストビューに文字を表示する方法のうち、xmlファイルに入力することで表示させる方法を2つ紹介します。
①テキストビューに直接入力する。
テキストビューに「android:text="表示させたい文字" 」を書き足すことで、表示されます。
<TextView
android:id="@+id/text2"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="表示させたい文字" />
②string.xmlからテキストを読み込む。
string.xmlにあらかじめ文字を入力しておき、テキストビューがその文字を読み込むように設定して表示させます。
「<string name="from_str">表示させたい文字</string>」
string.xmlは、res>valuesの中にあります。
上のテキストビューは直接入力し、下のテキストビューにはstring.xmlの文字を表示させてみます。
string.xmlの内容は、
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="from_str">string.xmlから表示</string>
<string name="app_name">ふわふわぷかぷか</string>
</resources>
<string name="app_name">ふわふわぷかぷか</string>はタイトルバーに表示されます。
main.xmlの内容は、
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
android:background="#ffffff">
<TextView
android:id="@+id/text1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="直接入力して表示" />
<TextView
android:id="@+id/text2"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/from_str" />
</LinearLayout>
同じように表示されているのでわかりにくいですが、実は違う方法で表示されています。
よくわかるAndroidアプリ開発の教科書 Android 4.2対応版
- 作者: 高見知英
- 出版社/メーカー: マイナビ
- 発売日: 2013/08/24
- メディア: 単行本(ソフトカバー)
- この商品を含むブログを見る