设置背景信息:
RelativeLayout layout = (RelativeLayout) findViewById(R.id.background);
layout.setBackgroundResource(R.drawable.ready);
这是最好的方法吗?
设置背景信息:
RelativeLayout layout = (RelativeLayout) findViewById(R.id.background);
layout.setBackgroundResource(R.drawable.ready);
这是最好的方法吗?
当前回答
试试ViewCompat。setBackground (yourView drawableBackground)
其他回答
RelativeLayout relativeLayout; //declare this globally
现在,在任何函数中,如onCreate, onResume
relativeLayout = new RelativeLayout(this);
relativeLayout.setBackgroundResource(R.drawable.view); //or whatever your image is
setContentView(relativeLayout); //you might be forgetting this
试试下面的代码:
Drawable thumb = ContextCompat.getDrawable(getActivity(), R.mipmap.cir_32);
mSeekBar.setThumb(thumb);
如果你使用AndroidX,你应该使用:
AppCompatResources.getDrawable(context, R.drawable.your_drawable)
不推荐使用前面列出的方法。
setBackground(getContext().getResources().getDrawable(R.drawable.green_rounded_frame));
试试这个。
int res = getResources().getIdentifier("you_image", "drawable", "com.my.package");
preview = (ImageView) findViewById(R.id.preview);
preview.setBackgroundResource(res);