如何在Android中以编程方式设置背景可绘制

Cha*_*ham 263 java android background drawable

设置背景:

RelativeLayout layout =(RelativeLayout)findViewById(R.id.background);
layout.setBackgroundResource(R.drawable.ready);
Run Code Online (Sandbox Code Playgroud)

这是最好的方法吗?

Laz*_*nja 465

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));
}
Run Code Online (Sandbox Code Playgroud)

但我认为问题出现是因为您正在尝试加载大图像.
是一个很好的教程如何加载大位图.

更新:
API级别22中不推荐使用的getDrawable(int)


getDrawable(int )现在已在API级别22中弃用.您应该使用支持库中的以下代码:

ContextCompat.getDrawable(context, R.drawable.ready)
Run Code Online (Sandbox Code Playgroud)

如果你引用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);
    }
}
Run Code Online (Sandbox Code Playgroud)

关于ContextCompat的更多细节

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

更新:
如果您使用的是支持v4库,则以下内容对所有版本都足够了.

ContextCompat.getDrawable(context, R.drawable.ready)
Run Code Online (Sandbox Code Playgroud)

您需要在app build.gradle中添加以下内容

compile 'com.android.support:support-v4:23.0.0' # or any version above
Run Code Online (Sandbox Code Playgroud)

或者在以下任何API中使用ResourceCompat:

import android.support.v4.content.res.ResourcesCompat;
ResourcesCompat.getDrawable(getResources(), R.drawable.name_of_drawable, null);
Run Code Online (Sandbox Code Playgroud)

  • 'getDrawable(int)'已弃用. (3认同)

Ahm*_*mad 103

试试这个:

layout.setBackground(ContextCompat.getDrawable(context, R.drawable.ready));
Run Code Online (Sandbox Code Playgroud)

对于API 16 <:

layout.setBackgroundDrawable(ContextCompat.getDrawable(context, R.drawable.ready));
Run Code Online (Sandbox Code Playgroud)

  • 你不需要`getResources().getDrawable()`.正确的代码是`layout.setBackgroundResource(R.drawable.ready);`就像使用的OP一样.这里的问题来自位图的大小. (38认同)
  • 啊好的,那我就会参考Lazy Ninjas的回答. (4认同)
  • 但这是同样的事情艾哈迈德:) (2认同)

Suj*_*mar 14

RelativeLayout relativeLayout;  //declare this globally
Run Code Online (Sandbox Code Playgroud)

现在,在onCreate,onResume等任何函数中

relativeLayout = new RelativeLayout(this);  
relativeLayout.setBackgroundResource(R.drawable.view); //or whatever your image is
setContentView(relativeLayout); //you might be forgetting this
Run Code Online (Sandbox Code Playgroud)


Fir*_*zen 11

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

AppCompatResources.getDrawable(context, R.drawable.your_drawable)
Run Code Online (Sandbox Code Playgroud)

以前列出的方法已被弃用。


小智 7

您还可以设置任何图像的背景:

View v;
Drawable image=(Drawable)getResources().getDrawable(R.drawable.img);
(ImageView)v.setBackground(image);
Run Code Online (Sandbox Code Playgroud)


小智 7

我正在使用 minSdkVersion 16 和 targetSdkVersion 23。
以下对我有用,它使用

ContextCompat.getDrawable(context, R.drawable.drawable);
Run Code Online (Sandbox Code Playgroud)

而不是使用:

layout.setBackgroundResource(R.drawable.ready);
Run Code Online (Sandbox Code Playgroud)

而是使用:

layout.setBackground(ContextCompat.getDrawable(this, R.drawable.ready));
Run Code Online (Sandbox Code Playgroud)

getActivity()用于片段中,如果从活动使用调用this