设置背景信息:

RelativeLayout layout = (RelativeLayout) findViewById(R.id.background);
layout.setBackgroundResource(R.drawable.ready);

这是最好的方法吗?


当前回答

setBackground(getContext().getResources().getDrawable(R.drawable.green_rounded_frame));

其他回答

setBackground(getContext().getResources().getDrawable(R.drawable.green_rounded_frame));

试试下面的代码:

Drawable thumb = ContextCompat.getDrawable(getActivity(), R.mipmap.cir_32);
mSeekBar.setThumb(thumb);

我正在使用minSdkVersion 16和targetSdkVersion 23。 以下是为我工作,它使用

ContextCompat.getDrawable(context, R.drawable.drawable);

而不是使用:

layout.setBackgroundResource(R.drawable.ready);

而使用:

layout.setBackground(ContextCompat.getDrawable(this, R.drawable.ready));

getActivity()在片段中使用,如果从活动调用则使用此方法。

试试ViewCompat。setBackground (yourView drawableBackground)

如果你使用AndroidX,你应该使用:

AppCompatResources.getDrawable(context, R.drawable.your_drawable)

不推荐使用前面列出的方法。