设置背景信息:

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

这是最好的方法吗?


试试这个:

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

对于API 16<:

layout.setBackgroundDrawable(ContextCompat.getDrawable(context, R.drawable.ready));

layout.setBackgroundResource (R.drawable.ready);是正确的。 另一种实现它的方法是使用以下方法:

final int sdk = android.os.Build.VERSION.SDK_INT;
if(sdk < android.os.Build.VERSION_CODES.JELLY_BEAN) {
    layout.setBackgroundDrawable(ContextCompat.getDrawable(context, R.drawable.ready) );
} else {
    layout.setBackground(ContextCompat.getDrawable(context, R.drawable.ready));
}

但我认为出现问题是因为你试图加载大图像。这是一个很好的教程如何加载大的位图。

更新:getDrawable(int)在API级别22中已弃用 getDrawable(int)现在在API级别22中已弃用。 你应该从支持库中使用下面的代码:

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

如果您参考ContextCompat的源代码。getDrawable,它会给你这样的东西:

/**
 * Return a drawable object associated with a particular resource ID.
 * <p>
 * Starting in {@link android.os.Build.VERSION_CODES#LOLLIPOP}, the returned
 * drawable will be styled for the specified Context's theme.
 *
 * @param id The desired resource identifier, as generated by the aapt tool.
 *            This integer encodes the package, type, and resource entry.
 *            The value 0 is an invalid identifier.
 * @return Drawable An object that can be used to draw this resource.
 */
public static final Drawable getDrawable(Context context, int id) {
    final int version = Build.VERSION.SDK_INT;
    if (version >= 21) {
        return ContextCompatApi21.getDrawable(context, id);
    } else {
        return context.getResources().getDrawable(id);
    }
}

关于ContextCompat的更多详细信息

从API 22开始,你应该使用getDrawable(int, Theme)方法而不是getDrawable(int)。

更新: 如果您使用的是support v4库,那么以下内容对于所有版本都足够了。

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

你需要在你的应用build.gradle中添加以下内容

compile 'com.android.support:support-v4:23.0.0' # or any version above

或者使用ResourceCompat,在任何API中,如下所示:

import android.support.v4.content.res.ResourcesCompat;
ResourcesCompat.getDrawable(getResources(), R.drawable.name_of_drawable, null);
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文件夹中,请尝试将图像从drawable文件夹移动到项目中的drawable-nodpi文件夹。这为我工作,似乎其他图像是由他们自己重新缩放。

你也可以设置任何图片的背景:

View v;
Drawable image=(Drawable)getResources().getDrawable(R.drawable.img);
(ImageView)v.setBackground(image);

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

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

而不是使用:

layout.setBackgroundResource(R.drawable.ready);

而使用:

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

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

使用butterknife将可绘制资源绑定到变量,方法是将其添加到类的顶部(在任何方法之前)。

@Bind(R.id.some_layout)
RelativeLayout layout;
@BindDrawable(R.drawable.some_drawable)
Drawable background;

然后在其中一个方法中添加

layout.setBackground(background);

这就是你所需要的

试试下面的代码:

Drawable thumb = ContextCompat.getDrawable(getActivity(), R.mipmap.cir_32);
mSeekBar.setThumb(thumb);
if (android.os.Build.VERSION.SDK_INT < Build.VERSION_CODES.JELLY_BEAN)
     layout.setBackgroundDrawable(getResources().getDrawable(R.drawable.ready));
else if(android.os.Build.VERSION.SDK_INT < Build.VERSION_CODES.LOLLIPOP_MR1)
     layout.setBackground(getResources().getDrawable(R.drawable.ready));
else
     layout.setBackground(ContextCompat.getDrawable(this, R.drawable.ready));

试试ViewCompat。setBackground (yourView drawableBackground)

在app/res/your_xml_layout_file.xml中

为父布局分配一个名称。 去MainActivity,通过调用findViewById(R.id."given_name")找到RelativeLayout。 通过调用setBackgroundColor()方法,将布局作为经典对象使用。

试试这个。

 int res = getResources().getIdentifier("you_image", "drawable", "com.my.package");
 preview = (ImageView) findViewById(R.id.preview);
 preview.setBackgroundResource(res);
setBackground(getContext().getResources().getDrawable(R.drawable.green_rounded_frame));

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

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

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