Android開発備忘録「日本語でおk」

自分が勉強した事を、忘れないようにメモする備忘録です。決して「他人様にお教えする」などというおこがましいものではありません。いつまでたっても覚えない自分の為のチラ裏ブログ

【基本中の基本を学ぶ】プロジェクト作成時のTextViewを見てみる

これがプロジェクト作成時のactivity_main.xmlの内容
最初の「Hello world!」を表示しているTextViewのみが配置されている状態。
 ↓

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
                xmlns:tools="http://schemas.android.com/tools"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:paddingLeft="@dimen/activity_horizontal_margin"
                android:paddingRight="@dimen/activity_horizontal_margin"
                android:paddingTop="@dimen/activity_vertical_margin"
                android:paddingBottom="@dimen/activity_vertical_margin"
                tools:context=".MainActivity">

    <TextView
        android:text="@string/hello_world"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"/>

</RelativeLayout>

ここの<TextView>要素中に書かれている

android:text="@string/hello_world"

により、strings.xmlの中に定義されている「hello_world」を呼び出している事が判る。
ちなみにstrings.xmlの中身はこれ
 ↓

<?xml version="1.0" encoding="utf-8"?>
<resources>

    <string name="app_name">TextView_Test</string>
    <string name="hello_world">Hello world!</string>
    <string name="action_settings">Settings</string>

</resources>

上から2番目の<string>要素に対し「hello_world」という名前のname属性を与えられているが、その中にHello world!のテキストの姿が見える。これがアクティビティ上に表示されている「Hello world!」の文字の正体だ。