记录一个Android方法,通过已知的图片名字,去获取这个图片在drawable中的ID,然后把他设置到ImageView中。
private ImageView imageView;
private void setImageViewBg() {
String imgStr = "aaaaa";//图片名字aaaaa
int resId = mContext.getResources().getIdentifier(
imgStr, "drawable", mContext.getPackageName());//通过名字获取ID
LogUtils.d(TAG, "resId is: " + imgStr);
//把图片设置到ImageView 中
imageView.setBackground(getResources().getDrawable(resId, null));
}
方法虽然简单,但是很实用。
另外通过图片ID获取图片名字:
String lastPictureName = getActivity().getResources().getResourceEntryName(R.drawable.xxxx);