setBackgroundDrawable() deprecated

前端 未结 11 1626
长发绾君心
长发绾君心 2020-12-03 00:18

So my sdk goes from 15 to 21 and when I call setBackgroundDrawable(), Android Studio tells me that it\'s deprecated.

I thought of going around it using:

相关标签:
11条回答
  • 2020-12-03 00:44

    This is correct in my case Solve this problem

     imageView.setBackgroundResource(images[productItem.getPosition()]);
    
    0 讨论(0)
  • 2020-12-03 00:47

    I'm using a minSdkVersion 16 and targetSdkVersion 23 The following is working for me, it uses ContextCompat.getDrawable(context, R.drawable.drawable);

    Instead of using: layout.setBackground(getResources().getDrawable(R.drawable.img_wstat_tstorm));

    Rather use:

    layout.setBackground(ContextCompat.getDrawable(getActivity(), R.drawable.img_wstat_tstorm));

    getActivity() is used in a fragment, if calling from a activity use this

    0 讨论(0)
  • 2020-12-03 00:51

    Use this:

    myView.background = ContextCompat.getDrawable(context, R.id.my_drawable)
    
    0 讨论(0)
  • 2020-12-03 00:55

    maybe you can try the following:

    setBackgroundResource(R.drawable.img_wstat_tstorm);
    
    0 讨论(0)
  • 2020-12-03 00:55
    BitmapDrawable background = new BitmapDrawable(BitmapFactory.decodeResource(getResources(), R.mipmap.Nome_imgem));
            getSupportActionBar().setBackgroundDrawable(background);
    
    0 讨论(0)
  • 2020-12-03 00:58

    It's funny because that method is deprecated, but if you look at the Android Source Code you'll find this:

       /**
         * Set the background to a given Drawable, or remove the background. If the
         * background has padding, this View's padding is set to the background's
         * padding. However, when a background is removed, this View's padding isn't
         * touched. If setting the padding is desired, please use
         * {@link #setPadding(int, int, int, int)}.
         *
         * @param background The Drawable to use as the background, or null to remove the
         *        background
         */
        public void setBackground(Drawable background) {
            //noinspection deprecation
            setBackgroundDrawable(background);
        }
    
    0 讨论(0)
提交回复
热议问题