设置背景:
RelativeLayout layout = (RelativeLayout) findViewById(R.id.background);
layout.setBackgroundResource(R.drawable.ready);
这是最好的方法吗?
RelativeLayout layout = (RelativeLayout) findViewById(R.id.background);
layout.setBackgroundResource(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));
}
我认为问题是由于你试图加载大图片导致的。
这里有一个好的教程,介绍如何加载大位图。
更新:
在API级别22中,getDrawable(int)已弃用 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)。
更新:
如果您正在使用支持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);
if(buttonBackground.equals(R.drawable.myDrawable))
,其中Drawable buttonBackground = myButton.getBackground();
但是我收到了这个错误:http://snag.gy/weYgA.jpg - Ruchir BaroniamyActivity.getTheme()
,而不是空参数:myView.setBackground(getResources().getDrawable(R.drawable.my_background, activity.getTheme()));
- ZonAppCompatResources.getDrawable(this.getContext(), resId)
,Google 已经在 AppCompat*
控件/视图中实现了它,例如:android.support.v7.widget.AppCompatCheckBox
。 - mochadwilayout.setBackground(ContextCompat.getDrawable(context, R.drawable.ready));
对于API 16及以下版本:
layout.setBackgroundDrawable(ContextCompat.getDrawable(context, R.drawable.ready));
getResources().getDrawable()
。正确的代码是 layout.setBackgroundResource(R.drawable.ready);
,就像原帖中所使用的一样。这里的问题来自于位图的大小。 - BVBRelativeLayout 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
我正在使用minSdkVersion 16和targetSdkVersion 23。
以下内容适用于我,它使用了
ContextCompat.getDrawable(context, R.drawable.drawable);
使用以下方式代替:
layout.setBackgroundResource(R.drawable.ready);
最好使用:
layout.setBackground(ContextCompat.getDrawable(this, R.drawable.ready));
getActivity()
用于片段中,如果从活动中调用,请使用 this
。
如果您使用 AndroidX,应使用:
AppCompatResources.getDrawable(context, R.drawable.your_drawable)
之前列出的方法已过时。
View v;
Drawable image=(Drawable)getResources().getDrawable(R.drawable.img);
(ImageView)v.setBackground(image);
(.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN)
。 - Armando Marques da S SobrinhosetBackground(getContext().getResources().getDrawable(R.drawable.green_rounded_frame));
使用butterknife将可绘制资源绑定到变量,通过在类的顶部添加以下内容(在任何方法之前)。
@Bind(R.id.some_layout)
RelativeLayout layout;
@BindDrawable(R.drawable.some_drawable)
Drawable background;
layout.setBackground(background);
那就是你所需要的全部
尝试使用 ViewCompat.setBackground(yourView, drawableBackground)
方法
remoteViews.setInt(R.id.btn_start,"setBackgroundResource", R.drawable.ic_button_start);
- Sam