前回やったことは、定義したファイルの取得とフォーマッターでした。
今回は前回では紹介できなかった、
画像、カラー、配列での変数管理について記します。
■サンプルソース
●xmlファイル
●javaファイル- 5
- 10
- 12
- リンゴ
- バナナ
- マンゴー
- 1.0f
- 2.5f
- 10.58f
- #FF0000
- #00FF00
- #0000FF
- @drawable/ic_launcher
- @drawable/logo
package com.example.Package;
import android.os.Bundle;
import android.app.Activity;
import android.content.res.TypedArray;
import android.graphics.Color;
import android.graphics.drawable.Drawable;
import android.view.ViewGroup;
import android.view.ViewGroup.LayoutParams;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//レイアウトの作成
LinearLayout oLayout = new LinearLayout(getApplicationContext());
//レイアウトにパーツを上から順に設置する設定
oLayout.setOrientation(LinearLayout.VERTICAL);
//レイアウトの設定
setContentView(oLayout);
//数値型配列の取得
int[] iAr = getResources().getIntArray(R.array.xml_int);
//文字列型配列の取得
String[] sAr = getResources().getStringArray(R.array.xml_string);
//浮動小数点型配列の取得
TypedArray tFloat = getResources().obtainTypedArray(R.array.xml_float);
//画像配列の取得
TypedArray tImage = getResources().obtainTypedArray(R.array.xml_images);
//色配列の取得
TypedArray tColor = getResources().obtainTypedArray(R.array.xml_color);
//レイアウトパラム定数(縦横の長さの定数)の格納
int WC = ViewGroup.LayoutParams.WRAP_CONTENT;
int MP = ViewGroup.LayoutParams.MATCH_PARENT;
//レイアウトの横幅・縦幅を設定
LayoutParams oLayPar = new LinearLayout.LayoutParams(WC, WC);
//数値型の表示
oLayout.addView(make_TextView(iAr[0]), oLayPar);
oLayout.addView(make_TextView(iAr[1]), oLayPar);
oLayout.addView(make_TextView(iAr[2]), oLayPar);
//文字列とお色の表示
oLayout.addView(make_TextView(sAr[0], tColor.getColor(0, 0)), oLayPar);
oLayout.addView(make_TextView(sAr[1], tColor.getColor(1, 0)), oLayPar);
oLayout.addView(make_TextView(sAr[2], tColor.getColor(2, 0)), oLayPar);
//浮動小数点型の表示
oLayout.addView(make_TextView(tFloat.getFloat(0, 0)), oLayPar);
oLayout.addView(make_TextView(tFloat.getFloat(1, 0)), oLayPar);
oLayout.addView(make_TextView(tFloat.getFloat(2, 0)), oLayPar);
//画像の表示
oLayout.addView(make_ImageView(tImage.getDrawable(0)), oLayPar);
oLayout.addView(make_ImageView(tImage.getDrawable(1)), oLayPar);
}
//イメージビューの作成
private ImageView make_ImageView(Drawable oBack){
//イメージビューの生成
ImageView iv = new ImageView(getApplicationContext());
//背景の設定
iv.setImageDrawable(oBack);
return iv;
}
//テキストビューの作成(背景色をつけます)
private TextView make_TextView(String sMessage, int iColor){
//テキストビューの生成
TextView tv = new TextView(getApplicationContext());
//背景色の設定
tv.setBackgroundColor(iColor);
//メッセージの設定
tv.setText(sMessage);
return tv;
}
//テキストビューの作成
private TextView make_TextView(String sMessage){
return make_TextView(sMessage, Color.TRANSPARENT);
}
//テキストビューの作成(long型用)
private TextView make_TextView(float fMessage){
return make_TextView(String.valueOf(fMessage));
}
//テキストビューの作成(int型用)
private TextView make_TextView(int iMessage){
return make_TextView(String.valueOf(iMessage));
}
}
■実行結果上手く出力できましたか?
xmlファイルの
@drawable/~
の場所は画像を用意して、res→drawable
に適当な画像ファイルを格納してください。
TypedArrayはいろいろなオブジェクトを配列に格納できます。
取り出すときは、TypedArray.get~(添字, エラー時の返し値);
で使えます。

0 件のコメント:
コメントを投稿