A.XMLファイルによるレイアウト / 画面レイアウトをリソースとして静的定義(XML)=静的リソースの変更

Android用語集

カテゴリー: レイアウト  閲覧数:439 配信日:2013-11-09 09:40


例1


XMLファイルをアクティビティに設定
setContentView(R.layout.test1_1);
package android.style;

import android.app.Activity;
import android.os.Bundle;

public class SetContentView1_1Activity extends Activity {
   /** Called when the activity is first created. */
   @Override
   public void onCreate(Bundle savedInstanceState) {
       super.onCreate(savedInstanceState);

       setContentView(R.layout.test1_1);// 「二つのボタンを追加したビューグループ」をアクティビティへ追加。具体的には、画面ビューを、「 レイアウトXMLファイルのリソースID」 で指定
   }
}


例2


「レイアウト用のリソースの中に記述された個々のビュー」に対してIDを設定(id="@+id/名前")
・2つのボタンと1つのテキストビューを設置し、それぞれのボタンがクリックされるとテキストビューにメッセージが表示されるプログラム
・画面レイアウト全体が1つのリソースとなっている場合には、画面レイアウトに配置された個々のビューに対してイベント処理を行おうとする場合、個々のビューをリソースの中から取り出す必要がある
   ↓
・ 「レイアウト用のリソースの中に記述された個々のビュー」に対してIDを設定 … id="@+id/名前"
・ 「ビューに対して割り当てられたリソースID」から対応するビューオブジェクトを取得 … Button jp_button = (Button)findViewById(R.id.jp_button);
▼D:/Android/workspace/SetContentView1_2.java
package android.style;

import android.app.Activity;
import android.os.Bundle;
import android.widget.Button;
import android.widget.TextView;
import android.view.View;

public class SetContentView1_2Activity extends Activity {// 2つのボタンと1つのテキストビューを設置し、それぞれのボタンがクリックされるとテキストビューにメッセージが表示されるプログラム
   /** Called when the activity is first created. */
   @Override
   public void onCreate(Bundle savedInstanceState) {
       super.onCreate(savedInstanceState);
       
       setContentView(R.layout.test6_1);// 「画面レイアウトをリソースとして定義した」レイアウト用xmlファイル」をプログラムの中から参照し、アクティビティへ設定

       Button jp_button = (Button)findViewById(R.id.jp_button);// ビューに対して割り当てられたリソースIDから対応するビューオブジェクトを取得。具体的にはソースの名前が「jp_button」のビューのオブジェクトを取得。戻り値は、そのビューのクラスでキャスト
       Button en_button = (Button)findViewById(R.id.en_button);
       final TextView msg_text = (TextView)findViewById(R.id.msg_text);

       jp_button.setOnClickListener(new View.OnClickListener() {
           public void onClick(View v) {
             msg_text.setText("こんにちは");
           }
       });

       en_button.setOnClickListener(new View.OnClickListener() {
           public void onClick(View v) {
             msg_text.setText("Hello");
           }
       });
   }
}


レイアウト用XMLファイル 作成
・ 「レイアウト用のリソースの中に記述された個々のビュー」に対してIDを設定 … id="@+id/名前"
・ @stringは「ハードコーティングに対する警告」対応
setContentView(R.layout.test6_1);
▼D:/Android/workspace/SetContentView1_2/res/layout/test6_1.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
   android:orientation="vertical"
   android:layout_width="fill_parent"
   android:layout_height="fill_parent"
   >
 <Button  
     android:id="@+id/jp_button"
     android:layout_width="wrap_content"
     android:layout_height="wrap_content"
     android:text="@string/Japanese"
     />
 <Button  
     android:id="@+id/en_button"
     android:layout_width="wrap_content"
     android:layout_height="wrap_content"
     android:text="@string/English"
     />
 <TextView  
     android:id="@+id/msg_text"
     android:layout_width="wrap_content"
     android:layout_height="wrap_content"
     android:text=""
     />
</LinearLayout>


ビューにIDを割り当てると、他のリソースと同じようにプロジェクトのビルド時にR.javaファイルに個々のビューに対するリソースIDが設定される
・ jp_button、 en_buttonなど
・ stringクラスのJapaneseなどは、「ハードコーティングに対する警告」対応の結果、設定されたもの
▼/SetContentView1_2/gen/android/style/R.java
/* AUTO-GENERATED FILE.  DO NOT MODIFY.
*
* This class was automatically generated by the
* aapt tool from the resource data it found.  It
* should not be modified by hand.
*/

package android.style;

public final class R {
   public static final class attr {
   }
   public static final class drawable {
       public static final int ic_launcher=0x7f020000;
   }
   public static final class id {
       public static final int en_button=0x7f050001;
       public static final int jp_button=0x7f050000;
       public static final int msg_text=0x7f050002;
   }
   public static final class layout {
       public static final int main=0x7f030000;
       public static final int test6_1=0x7f030001;
   }
   public static final class string {
       public static final int English=0x7f040003;
       public static final int Japanese=0x7f040002;
       public static final int app_name=0x7f040001;
       public static final int hello=0x7f040000;
   }
}



レイアウトXMLファイルを削除して、JAVAだけでレイアウト定義できる?
レイアウトファイルを使わないでコードでTextViewを作る