【问题标题】:How do you obtain a Drawable object from a resource id in android package?如何从 android 包中的资源 id 获取 Drawable 对象?
【发布时间】:2011-12-10 13:40:22
【问题描述】:

我需要一个 Drawable 对象以显示在图像按钮上。有没有办法使用下面的代码(或类似的代码)从 android.R.drawable.* 包中获取对象?

例如,如果 drawableId 是 android.R.drawable.ic_delete

mContext.getResources().getDrawable(drawableId)

【问题讨论】:

    标签: android resources drawable android-context


    【解决方案1】:
    Drawable d = getResources().getDrawable(android.R.drawable.ic_dialog_email);
    ImageView image = (ImageView)findViewById(R.id.image);
    image.setImageDrawable(d);
    

    【讨论】:

    • 我还发现使用应用程序上下文似乎可以工作,谢谢。
    • 从 API 22 开始。getDrawable(int id) 已弃用。请改用getDrawable(int id, Resources.Theme theme)。方法getTheme()应该会有帮助。
    • 我有一点疑问。在这段代码中,“资源类型中的方法 getDrawable(int) 已弃用”。根据一个 SO 回答 1. 在 Java 中使用不推荐使用的方法或类是错误的吗?从 deprecated 的定义:“注解 @Deprecated 的程序元素是不鼓励程序员使用的程序元素,通常是因为它很危险,或者因为存在更好的替代方案。”有什么更好的选择。
    【解决方案2】:

    API 21 开始,您应该使用 getDrawable(int, Theme) 方法而不是 getDrawable(int),因为它允许您获取与给定的特定 resource ID 关联的 drawable 对象screen density/theme。调用deprecatedgetDrawable(int)方法相当于调用getDrawable(int, null)

    您应该改用支持库中的以下代码:

    ContextCompat.getDrawable(context, android.R.drawable.ic_dialog_email)
    

    使用该方法相当于调用:

    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
        return resources.getDrawable(id, context.getTheme());
    } else {
        return resources.getDrawable(id);
    }
    

    【讨论】:

    • context.getDrawable(id); 似乎等同于resources.getDrawable(id, context.getTheme());
    • 如果您有支持库,这可以在一行中完成:ResourcesCompat.getDrawable(resources, id, context.getTheme());
    【解决方案3】:

    从 API 21 开始,您还可以使用:

       ResourcesCompat.getDrawable(getResources(), R.drawable.name, null);
    

    而不是ContextCompat.getDrawable(context, android.R.drawable.ic_dialog_email)

    【讨论】:

    • 您能否对选择提供进一步的解释
    【解决方案4】:

    最好的办法是

     button.setBackgroundResource(android.R.drawable.ic_delete);
    

    这个用于 Drawable 左侧,类似的用于右侧等。

    int imgResource = R.drawable.left_img;
    button.setCompoundDrawablesWithIntrinsicBounds(imgResource, 0, 0, 0);
    

    getResources().getDrawable() 现已弃用

    【讨论】:

      【解决方案5】:

      从 API 21 `getDrawable(int id)` 被弃用

      所以现在你需要使用

      ResourcesCompat.getDrawable(context.getResources(), R.drawable.img_user, null)
      

      但最好的办法是:

      - 您应该创建一个通用类来获取可绘制对象和颜色,因为如果将来有任何弃用,那么您无需在项目中的任何地方进行更改。您只需更改此方法
      import android.content.Context
      import android.graphics.drawable.Drawable
      import androidx.core.content.res.ResourcesCompat
      
      object ResourceUtils {
          fun getColor(context: Context, color: Int): Int {
              return ResourcesCompat.getColor(context.resources, color, null)
          }
      
          fun getDrawable(context: Context, drawable: Int): Drawable? {
              return ResourcesCompat.getDrawable(context.resources, drawable, null)
          }
      }
      

      像这样使用这个方法:

      Drawable img=ResourceUtils.getDrawable(context, R.drawable.img_user)
      image.setImageDrawable(img);
      

      【讨论】:

        【解决方案6】:

        遵循 Kotlin 程序员的解决方案(来自 API 22)

        val res = context?.let { ContextCompat.getDrawable(it, R.id.any_resource }
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2017-08-09
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2023-04-02
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多