Android疑难杂症-根据资源名称获取资源Id

  1. 1. 应用场景
  2. 2. 工具封装
1
2
Resources resources = context.getResources();
int id= getResources().getIdentifier("图标名称(不带后缀)", "drawable", "包名");

应用场景

  • 用数字图片表示剩余天数
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
private void setLeftDays(int days) {
String dayStr = Integer.toString(days);
llLeftDays.removeAllViews();
for (int i=dayStr.length()-1; i>0; i--) {
int d = (int) (days / (Math.pow(10, i)));
int s = d%10;
ImageView iv = new ImageView(context);
iv.setImageResource(getResources().getIdentifier("image_number"+s, "drawable", "me.zhouzhuo810.test"));
iv.setLayoutParams(new LinearLayout.LayoutParams(ViewUtil.scaleTextValue(context, 52)
,ViewUtil.scaleTextValue(context, 64)));
llLeftDays.addView(iv);
}
int l = days%10;
ImageView iv = new ImageView(context);
iv.setImageResource(getResources().getIdentifier("image_number"+l, "drawable", "me.zhouzhuo810.test"));
iv.setLayoutParams(new LinearLayout.LayoutParams(ViewUtil.scaleTextValue(context, 52)
,ViewUtil.scaleTextValue(context, 64)));
llLeftDays.addView(iv);
}

工具封装

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
/**
* 资源文件工具类
*/
public class ResourcesUtils {

private static final String RES_ID = "id";
private static final String RES_STRING = "string";
private static final String RES_DRABLE = "drable";
private static final String RES_LAYOUT = "layout";
private static final String RES_STYLE = "style";
private static final String RES_COLOR = "color";
private static final String RES_DIMEN = "dimen";
private static final String RES_ANIM = "anim";
private static final String RES_MENU = "menu";


/**
* 获取资源文件的id
* @param context
* @param resName
* @return
*/
public static int getId(Context context,String resName){
return getResId(context,resName,RES_ID);
}

/**
* 获取资源文件string的id
* @param context
* @param resName
* @return
*/
public static int getStringId(Context context,String resName){
return getResId(context,resName,RES_STRING);
}

/**
* 获取资源文件drable的id
* @param context
* @param resName
* @return
*/
public static int getDrableId(Context context,String resName){
return getResId(context,resName,RES_DRABLE);
}

/**
* 获取资源文件layout的id
* @param context
* @param resName
* @return
*/
public static int getLayoutId(Context context,String resName){
return getResId(context,resName,RES_LAYOUT);
}

/**
* 获取资源文件style的id
* @param context
* @param resName
* @return
*/
public static int getStyleId(Context context,String resName){
return getResId(context,resName,RES_STYLE);
}

/**
* 获取资源文件color的id
* @param context
* @param resName
* @return
*/
public static int getColorId(Context context,String resName){
return getResId(context,resName,RES_COLOR);
}

/**
* 获取资源文件dimen的id
* @param context
* @param resName
* @return
*/
public static int getDimenId(Context context,String resName){
return getResId(context,resName,RES_DIMEN);
}

/**
* 获取资源文件ainm的id
* @param context
* @param resName
* @return
*/
public static int getAnimId(Context context,String resName){
return getResId(context,resName,RES_ANIM);
}

/**
* 获取资源文件menu的id
*/
public static int getMenuId(Context context,String resName){
return getResId(context,resName,RES_MENU);
}

/**
* 获取资源文件ID
* @param context
* @param resName
* @param defType
* @return
*/
public static int getResId(Context context,String resName,String defType){
return context.getResources().getIdentifier(resName, defType, context.getPackageName());
}
}