LoginSignup
2
2

More than 5 years have passed since last update.

Android xml arrayでdrawableの画像を設定する

Last updated at Posted at 2016-10-28

忘備録がてら書いたので、ソースコードは省略しています。

array.xml

itemタグの中にdrawableの画像が設定できる

array.xml
<array name="array_hoge_1">
        <item>@drawable/hoge_1</item>
        <item>@drawable/hoge_2</item>
        <item>@drawable/hoge_3</item>
        <item>@drawable/hoge_4</item>
</array>

hoge.java

array.xmlのresource idをdrawableIds[]に格納している
TypedArray #recycle()は必須

hoge.java
TypedArray typedArray 
    = context.getResources().obtainTypedArray(R.array.array);
int[] drawableIds = new int[typedArray.length()];
for (int i = 0; i < typedArray.length(); i++){
    // drawableのresource idを取得
    drawableIds[i] = typedArray.getResourceId(i, 0);
}
 typedArray.recycle();

ImageViewにセットする

hoge.java
imageView.setImageResource(drawableIds[i]);
2
2
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
2