■定義できるデータやファイル
- Color(res/values)
- fontやbackgroundの色を指定する数値
- String(res/values)
- 表示する文字列
- Dimensions
- 単位付きのサイズ
- Drawable(res/drawable/)
- 画像
- Animation(res/anim/)
- SVG的な感じ
- Layout(res/layout/)
- Viewの位置や大きさを定義。HTML的なXML
- Style, Theme(res/values)
- レイアウト用の属性値を抜き出したようなもの
androidでは無理してcodeに書かない限り、Viewは分離される。
- res/values/
- 文字列
- res/xml/
- 任意のXMLファイル
- res/raw
- デバイスにコピーされるファイル
■リソースへのアクセス
eclipseのPackeage Explorer上でファイルを追加する。Rクラスに自動的に追加されたidentifer(staticフィールド)を通してアクセスする。
プログラムからアクセス
setContentView(R.layout.main);
spec = tabHost.newTabSpec("tab1").setIndicator(getString(R.string.favorite), res.getDrawable(R.drawable.star)).setContent(intent);
つまり以下のようになる。
String title = getString(R.string.favorite); //String title = getResources().getString(R.string.favorite);
Drawable icon = getResources().getDrawable(R.drawable.ic_launcher);
リソースファイル間でのアクセス
以下のような形式でアクセスすることができる。
@[package:]type/name
例
res/values/color.xml
以下のようにアプリケーション共通で使う色を定義する。
<?xml version="1.0" encoding="utf-8"?> <resources> <color name="red">#ff0000</color> </resources>
res/layout/mail.xml
以下のように使用する。
<TextView android:layout_width="fill_parent" android:layout_height="wrap_content" android:textColor="@color/red" />
またプログラムから取得するには以下のようにする。
getResouce().getColor(R.color.red);
■ローカライズ
表示する文字は以下のようにして多言語対応ができる。
- res/values/string.xml
- 英語文字列を定義したファイル
- res/values-ja/string.xml
- 日本語文字列を定義したファイル
■マルチデバイス
android端末は画面のサイズがまちまちだ。本当にまちまちだ。とりあえず以下のようにしてディスプレイの解像度に合わせた画像を用意することができる。
- res/drawable-hdpi/*.(png|jpg|gif)
- 高解像度用画像ファイル
- res/drawable-mdpi/*.(png|jpg|gif)
- 中解像度用画像ファイル
- res/drawable-ldpi/*.(png|jpg|gif)
- 低解像度用画像ファイル
但し、androidではpngが推奨される。
■アニメーション
以下のようにしてアニメーションを定義することができる。
res/anim/rotate.xml
<?xml version="1.0" encoding="utf-8"?> <rotate xmlns:android="http://schemas.android.com/apk/res/android" android:fromDegrees="0" android:toDegrees="180" android:pivotX="100" android:pivotY="100" android:duration="1000" android:repeatCount="10"> </rotate>
以下のようなレイアウトがあった場合、
<?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" > <ImageView android:layout_width="wrap_content" android:layout_height="wrap_content" android:src="@drawable/ic_launcher" android:id="@+id/icon" /> </LinearLayout>
以下のようにする事でアニメーションを適用できる。
Animation animation = AnimationUtils.loadAnimation(this, R.anim.rotate); ImageView imageView = (ImageView) findViewById(R.id.icon); imageView.startAnimation(animation);
■Shape
以下のようにして形状を定義することもできる。
<?xml version="1.0" encoding="utf-8"?> <shape android:shape="rectangle" xmlns:android="http://schemas.android.com/apk/res/android"> <solid android:color="#ccffffff" /> <corners android:topLeftRadius="10.0dip" android:topRightRadius="10.0dip" android:bottomLeftRadius="10.0dip" android:bottomRightRadius="10.0dip" /> </shape>
以下のようにして使用する。
<LinearLayout android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="wrap_content" android:background="@drawable/round" > </LinearLayout>