试试这个:
public int getColorByName( String name ) {
int colorId = 0;
try {
Class res = R.color.class;
Field field = res.getField( name );
colorId = field.getInt(null);
} catch ( Exception e ) {
e.printStackTrace();
}
return colorId;
}
Run Code Online (Sandbox Code Playgroud)
在你的情况下name是my_color:
getColorByName("my_color");
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
1887 次 |
| 最近记录: |