Java 如何在Android中以编程方式设置后台绘图 要设置背景,请执行以下操作:

Java 如何在Android中以编程方式设置后台绘图 要设置背景,请执行以下操作:,java,android,background,drawable,Java,Android,Background,Drawable,最好的方法是什么?试试以下方法: layout.setBackground(ContextCompat.getDrawable(context, R.drawable.ready)); 对于API16布局,setBackgroundResource(R.drawable.ready)正确。 实现这一目标的另一种方法是使用以下方法: final int sdk = android.os.Build.VERSION.SDK_INT; if(sdk < android.os.Build.VER

最好的方法是什么?

试试以下方法:

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

对于API16
布局,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));
}
如果您参考的源代码,它会提供如下内容:

/**
 * 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);
    }
}
您需要在应用程序build.gradle中添加以下内容

compile 'com.android.support:support-v4:23.0.0' # or any version above
或在任何API中使用ResourceCompat,如下所示:

import android.support.v4.content.res.ResourcesCompat;
ResourcesCompat.getDrawable(getResources(), R.drawable.name_of_drawable, null);
现在,在像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 Nodepi文件夹。这对我来说很有效,似乎其他图像都是由它们自己重新缩放的。

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

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

我使用的是MinSDK版本16和TargetSDK版本23。
以下内容适用于我,它使用

ContextCompat.getDrawable(context, R.drawable.drawable);
而不是使用:

layout.setBackgroundResource(R.drawable.ready);
而是使用:

layout.setBackground(ContextCompat.getDrawable(this, R.drawable.ready));
getActivity()
在片段中使用,如果从活动调用,则使用
this
通过将其添加到类的顶部(在任何方法之前),将可绘制资源绑定到变量

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

layout.setBackground(background);
这就是您所需要的全部

请尝试以下代码:

Drawable thumb = ContextCompat.getDrawable(getActivity(), R.mipmap.cir_32);
mSeekBar.setThumb(thumb);
if(android.os.Build.VERSION.SDK\u INT
尝试
查看compat.setBackground(yourView,drawableBackground)
在应用程序内/res/查看您的xml布局文件

  • 为父布局指定一个名称
  • 转到您的主要活动,通过调用findViewById(R.id.“给定的名称”)找到您的亲戚您
  • 通过调用setBackgroundColor()方法,将布局用作经典对象
  • 试试这个

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

    但是这和艾哈迈德是一样的:)啊,好吧,那么我会参考懒惰忍者的答案。你不需要
    getResources().getDrawable()
    。正确的代码是
    layout.setBackgroundResource(R.drawable.ready)就像使用的OP一样。这里的问题来自位图的大小。setBackground仅为API级别16或更高。如果您没有高清质量的项目中需要使用的图像副本,为什么让android使用普通的可绘制文件夹将其重新缩放到糟糕的质量。即使这个问题是老问题,如果它仍然在谷歌中弹出,那么在它里面发布一些新的东西也没问题,因为“getDrawable(int)”是不推荐的。嘿,我只是想在一个图像按钮的背景图像是一个特定的可绘制资源的情况下才去做一个任务。我怎样才能比较。。。我尝试了
    if(buttonBackground.equals(R.drawable.myDrawable))
    where
    drawable buttonBackground=myButton.getBackground()我得到这个错误:您还需要
    myActivity.getTheme()
    来获取最新版本的方法,而不是空参数:
    myView.setBackground(getResources().getDrawable(R.drawable.my_background,activity.getTheme())
    或者你可以使用
    AppCompatResources.getDrawable(this.getContext(),resId)
    代替,Google已经在
    AppCompat*
    widget/view中实现了它,例如:
    android.support.v7.widget.AppCompatCheckBox
    这解决了我的问题,但我需要实现
    (.SDK\INT>=Build.VERSION\code.JELLY\u BEAN)
    内部代码现在不推荐使用此代码谢谢!您的问题和所有有用的答案帮助我在小部件中设置了图像按钮的背景资源。下面是一个示例代码,以防有人感兴趣:
    remoteViews.setInt(R.id.btn_start,“setBackgroundResource”,R.drawable.ic_button_start)Kotlin解决方案,可能需要:广告解释,请。
    
    layout.setBackgroundResource(R.drawable.ready);
    
    layout.setBackground(ContextCompat.getDrawable(this, R.drawable.ready));
    
    @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));
    
     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));