Androidの授業 4 [ Javaで書いたレイアウトをxmlへ落とし込む ]
【Javaで書いたレイアウトをxmlへ落とし込む】
新規プロジェクト"JavaXml"作成
パッケージ:test.sample
Activity :MainActivity
①Javaでレイアウト
JavaXml→src→sample.test→MainActivityを開く。
---------------------------------------------------
package sample.test;
import android.app.Activity;
import android.os.Bundle;
public class MainActivity extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
}
}
----------------------------------------------------
・setContentView(R.layout.main); の上の行に追加。
//LinearLayoutクラスのインスタンス化(一行に並べる為のレイアウトの準備)
LinearLayout layout = new LinearLayout(this);
(この時点で左に×(エラー)が付くのでLinearLayoutクラスをインポート)
//setOrientationメソッドで配置を縦に指定
layout.setOrientation(Linearlayout.VERTICAL);
//setBackgroundColorメソッドで背景色を白に指定(→Colorをインポート)
layout.setBackgroundColor(Color.WHITE);
・setContentView(R.layout.main); の書き換え
//画面に作成したレイアウトを設定する
setContentView(layout);
・その下に追記していく内容
//文字を表示するためのUIの準備
TextView textView = new TextView(this);
//textViewに文字を設定する
textView.setText("テキストビュー");
//文字色の指定
textView.setTextColor(Color.BLACK);
//レイアウトにtextViewを配置する
layout.addView(textCiew);
・画像の配置
//画像を表示するためのUIを用意する
ImageView imageView = new ImageView(this); (→ImageViewをインポート)
//画像を設定する
imageView.setImageResource(R.drawable.icon); (デフォルトの画像"icon")
//レイアウトにimageViewを配置する
layout.addView(imageView);
②同じ内容をxmlの操作で行う
一旦今やった作業内容のうちsetContentViewをコメントアウト。
//setContentView(layout);
setContentView(R.layout.main); を追記
(layoutの設定が影響しなくなる)
JavaXml→res→layout→main.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"
>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/hello"
/>
</LinearLayout>
-------------------------------------------------
3行目~最終行の1行前までを書き換え。
-------------------------------------------------
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android
"
>
</LinearLayout>
-------------------------------------------------
↑この状態(Linearlayoutの宣言のみ)から追記していく。
-------------------------------------------------
<?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"
>
<--! 上記までがLinearLayout内の設定。-->
<--! 文字の表示。LinearLayout内にTextViewを配置する -->
<TextView
<--! 幅の設定(最大)-->
android:layout_width="fill_parent"
<--! 高さの設定(最小)-->
android:layout_height="wrap_content"
<--! 文字の設定 -->
android:text="テキストビュー"
<--! 文字色の設定(黒) -->
android:textColor="#000000"
></TextView>
<--! 画像の表示。LinearLayout内にImageViewを配置 -->
<ImageView
<--! 幅、高さの設定 -->
android:layout_width="fill_parent"
android:layout_height="wrap_content"
<--! 画像の設定 -->
android:src="@drawable/icon"
></ImageView>
</LinearLayout>
③Strings.xmlで文字列を管理する。
JavaXml→res→values→Strings.xml を開く
-----------------------------------------
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="hello">Hello World, MainActivity!</string>
<string name="app_name">JavaXml</string>
</resources>
-----------------------------------------
</resources>の上に追記していく。
<string name="sample_text">テキストビュー</string>
これにより、"sample_text"という名前で呼び出せるstring"テキストビュー"が追加される。
この状態で②のテキスト表示の部分を変更。
android:text="テキストビュー"
↓
android:text="@string/sample_text"
④色の設定(color.xml)を追加してみる。
valuesの中にxmlを新規作成。(なければその他から。)
名前をcolor.xmlに設定。
追加→color
Nameにwhite
Valueに#FFFFFF を入力。
color.xmlに直接リソースの追記でもOK。
<color name="white">#FFFFFF</color>
同様にblack #000000 を追加してみる。
この状態で②の背景色指定、文字色指定の部分を変更。
android:background="@color/white"
android:textColor="@color/black"