AndroidのResource - リソースの概要
Androidアプリのソースは大きく三つの種類がある。
- レイアウトファイル
- javaソースファイル
- リソースファイル
リソースファイルの種類
リソースのアクセス
// windowの背景にR.drawable.img1を設定する getWindow().setBackgroundDrawableResource(R.drawable.img1); // タイトルにR.string.sample_stringを設定する getWindow().setTitle(getResources().getText(R.string.sample_string)); // TextViewコンポーネントを取得する TextView text = (TextView) findViewById(R.id.textview);
Resources res = getResources(); // 文字列取得 String sampleString = res.getText(R.string.sample_string).toString(); // Drawableリソース取得 Drawable img = res.getDrawable(R.drawable.img1, getTheme());
- xml内でリソース使用
@[<package_name>:]<resource_type>/<resrouce_name>
<?xml version="1.0" encoding="utf-8"?> <resources> <color name="colorPrimary">#3F51B5</color> <color name="colorPrimaryDark">#303F9F</color> <color name="colorAccent">#FF4081</color> </resources> <resources> <string name="app_name">GestureLibrarySample</string> <string name="sample_string">SAMPLE_STRING</string> </resources> <EditText android:layout_width="match_parent" android:layout_height="match_parent" android:textColor="@color/colorAccent" android:text="@string/sample_string"/>