//获取连连看所有图片的ID(约定所有图片ID以p_开头)
public static List<Integer> getImageValues()
{Log.e("hyz", "ImageUtil:getImageValues()");
try
{
// 得到R.drawable所有的属性, 即获取drawable目录下的所有图片
Field[] drawableFields = R.drawable.class.getFields();
List<Integer> resourceValues = new ArrayList<Integer>();
for (Field field : drawableFields)
{
// 如果该Field的名称以p_开头,如果要检索的字符串值没有出现,则该方法返回 -1。
if (field.getName().indexOf("p_") != -1)
{
resourceValues.add(field.getInt(R.drawable.class));
}
}
return resourceValues;
}
catch (Exception e)
{
return null;
}
}
点击某图换背景:
<ImageView
android:id="@+id/btn_add"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
style="@style/globaltime_button_add"/>
values/styles.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
<style name="globaltime_button_add">
<item name="android:background">@drawable/button_add</item>
<item name="android:src">@drawable/ic_menu_add</item>
</style>
</resources>
drawable//button_add.xml
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed="true"
android:drawable="@drawable/btn_bg_pressed" />
<item android:drawable="@drawable/bottom_bar_bg" />
</selector>