【问题标题】:setBackgroundDrawable() deprecated不推荐使用 setBackgroundDrawable()
【发布时间】:2015-01-24 07:35:31
【问题描述】:

所以我的 sdk 从 15 变为 21,当我调用 setBackgroundDrawable() 时,Android Studio 告诉我它已被弃用。

我想用以下方法绕过它:

int sdk = android.os.Build.VERSION.SDK_INT;

if(sdk < android.os.Build.VERSION_CODES.JELLY_BEAN) {
    layout.setBackgroundDrawable(getResources().getDrawable(R.drawable.img_wstat_tstorm));
} else {
    layout.setBackground(getResources().getDrawable(R.drawable.img_wstat_tstorm));
}

然后,我在“setBackground()”处收到错误。

那么,你会怎么处理呢?

【问题讨论】:

  • 您收到错误或警告吗?
  • manifest 中 min sdk version 的值是多少?
  • 使用 setbackgroundresource(R.drawable.img_wstat_tstorm);对于更高版本。setBackgroundDrawable 在更高版本中被贬低,希望对您有所帮助
  • Min sdk 为 15。我在红色下划线的“setBackground()”但应用程序运行,所以我猜这是一个警告
  • 你必须得到 Add @SupressWarning

标签: android deprecated deprecation-warning


【解决方案1】:

这是一个有趣的话题。显然,您这样做的方式是正确的。这实际上只是一个命名决定的改变。正如this answer 指出的那样,setBackground() 只需调用setBackgroundDrawable()

public void setBackground(Drawable background) {
    //noinspection deprecation
    setBackgroundDrawable(background);
}

@Deprecated
public void setBackgroundDrawable(Drawable background) { ... }

您可以查看this thread 了解有关所有这些的更多信息。

【讨论】:

  • 您应该注意 setBackground() 不适用于 API16 之前的版本,替代方案可以是 setBackgroundResource
【解决方案2】:

也许您可以尝试以下方法:

setBackgroundResource(R.drawable.img_wstat_tstorm);

【讨论】:

    【解决方案3】:

    这很有趣,因为该方法已被弃用,但如果您查看 Android 源代码,您会发现:

       /**
         * 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);
        }
    

    【讨论】:

      【解决方案4】:

      截至 2018 年 8 月 15 日正确

      使用支持库

      Drawable drawable = ResourcesCompat.getDrawable(getResources(), drawableRes, null);
      ViewCompat.setBackground(layout, drawable);
      

      【讨论】:

        【解决方案5】:

        您收到错误是因为 getResources().getDrawable() 将 id (int) 而非可绘制对象作为其参数。试试这个:

        layout.setBackground(getResources().getDrawable(R.id.img_wstat_tstorm));

        【讨论】:

        • setBackground 需要 Drawable 仅 ID
        • 你错了。来自 API 文档:android.view.View.setBackground(可绘制背景);参数:background 用作背景的 Drawable,或 null 移除背景。
        【解决方案6】:
        //Java
        view.setBackground(ActivityCompat.getDrawable(context, R.drawable.bg))
        
        //Kotlin 
        view.background = ActivityCompat.getDrawable(context, R.drawable.bg)
        

        【讨论】:

          【解决方案7】:

          使用这个:

          myView.background = ContextCompat.getDrawable(context, R.id.my_drawable)
          

          【讨论】:

            【解决方案8】:

            在我的情况下这是正确的 解决这个问题

             imageView.setBackgroundResource(images[productItem.getPosition()]);
            

            【讨论】:

              【解决方案9】:

              截至 2018 年 11 月 23 日正确

              科特林:

              view.background = resources.getDrawable(R.drawable.ic_image,theme)
              

              如果包含 Theme 参数。

              【讨论】:

                【解决方案10】:

                我正在使用 minSdkVersion 16 和 targetSdkVersion 23 以下对我有用,它使用 ContextCompat.getDrawable(context, R.drawable.drawable);

                而不是使用:layout.setBackground(getResources().getDrawable(R.drawable.img_wstat_tstorm));

                相当使用:

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

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

                【讨论】:

                • 对 minSdk 15 提出问题
                【解决方案11】:
                BitmapDrawable background = new BitmapDrawable(BitmapFactory.decodeResource(getResources(), R.mipmap.Nome_imgem));
                        getSupportActionBar().setBackgroundDrawable(background);
                

                【讨论】:

                • 如果你能用几句话总结你在这里想要做的事情真的会有所帮助......
                猜你喜欢
                • 1970-01-01
                • 1970-01-01
                • 2016-04-22
                • 2012-11-14
                • 2019-11-08
                • 2019-11-08
                • 2020-01-03
                • 2011-12-02
                • 2020-06-08
                相关资源
                最近更新 更多