【问题标题】:Calling setCompoundDrawables() doesn't display the Compound Drawable调用 setCompoundDrawables() 不会显示 Compound Drawable
【发布时间】:2011-09-29 06:28:02
【问题描述】:

我调用setCompoundDrawables方法后,复合Drawable没有显示出来..

Drawable myDrawable = getResources().getDrawable(R.drawable.btn);
btn.setCompoundDrawables(myDrawable, null, null, null);

有什么想法吗?

【问题讨论】:

  • 如以下答案所述,需要调用名为 (..)WithIntrinsicBounds 的方法的变体。附带说明一下,Compound Drawable 的padding 必须在之后设置此调用以产生效果
  • document 说:Drawables 必须已经调用了setBounds(Rect)
  • 嗨,hunterp,刚刚在咖啡店 (Angel) 认识你,现在我知道你知道 Android Drawable 是什么了(也许你在使用它们中的许多错误时已经因为错误错误而记错了) ,我可以告诉你一些我合作过的项目不得不处理这个问题,看看毕加索(github.com/square/picasso)使用的github.com/JakeWharton/DiskLruCache(我合作使Android更友好)
  • @Dr1Ku 其实我以前有过,反正也可以用。

标签: android android-layout android-drawable


【解决方案1】:

【讨论】:

  • 需要 api 17 所以 Drawable.setBounds() 可能会更好
  • 非常感谢..这对我有用..我可以知道这两者有什么区别吗?
  • @user1324936 'relative' 版本需要 API 17,其他版本可以使用以前的版本
  • @user1324936 setCompoundDrawablesWithIntrinsicBounds 已添加到 API 级别 3
  • 我使用的是 setCompoundDrawableRelativeWithIntrinsicBounds
【解决方案2】:

使用这个(我测试过)。效果不错

Drawable image = context.getResources().getDrawable( R.drawable.ic_action );
int h = image.getIntrinsicHeight(); 
int w = image.getIntrinsicWidth();   
image.setBounds( 0, 0, w, h );
button.setCompoundDrawables( image, null, null, null );

【讨论】:

  • 这在针对低于 17 的 API 时很有用,因为 EditText#setCompoundDrawablesWithIntrinsicBounds 至少需要 API 17。
  • 你能提供一个来源吗?我看到的所有文档都表明这是available since API 1
【解决方案3】:

图像是空白的,因为它没有指定的边界。您可以使用setCompoundDrawables(),但在指定图像边界之前,请使用Drawable.setBounds() 方法

【讨论】:

  • 最佳答案,因为您实际上提供了为什么 setBounds 很重要的原因。
  • @Andy 没错,讨厌这些 800 票的热门答案,他们只是复制粘贴了一行代码而没有任何文字
【解决方案4】:

示例置顶:

view.setCompoundDrawablesWithIntrinsicBounds(
    null,
    getResources().getDrawable(R.drawable.some_img),
    null,
    null
);

参数顺序:(左、上、右、下)

【讨论】:

  • 这应该是公认的答案,在我的情况下使用按钮。它按预期工作!!!它还向后兼容。
【解决方案5】:

再简单一点:

Drawable image = context.getResources().getDrawable(R.drawable.ic_action );
image.setBounds( 0, 0, image.getIntrinsicWidth(), image.getIntrinsicHeight() );
button.setCompoundDrawables( image, null, null, null );

【讨论】:

    【解决方案6】:

    图像未显示,因为您没有指定边界,所以这里有 2 个选项。

    第一种方法

    使用setCompoundDrawablesWithIntrinsicBounds方法,如下图

    Drawable myDrawable = getResources().getDrawable(R.drawable.btn);
    btn. setCompoundDrawablesWithIntrinsicBounds(myDrawable, null, null, null);
    

    第二种方法

    你可以在应用到TextView之前给drawable应用边界,如下图

    Drawable myDrawable = getResources().getDrawable(R.drawable.btn);
    myDrawable.setBounds( 0, 0, myDrawable.getIntrinsicWidth(), myDrawable.getIntrinsicHeight());
    btn.setCompoundDrawables(myDrawable, null, null, null);
    

    就是这样。

    【讨论】:

      【解决方案7】:

      在 API 22 中已弃用。

      这段代码对我有用:

      Drawable drawable = ResourcesCompat.getDrawable(getResources(),R.drawable.wen, null);
      drawable.setBounds(0, 0, drawable.getMinimumWidth(),
      drawable.getMinimumHeight());
      tv.setCompoundDrawables(drawable, null, null, null);
      

      【讨论】:

        【解决方案8】:

        在 Kotlin 中:

        1) 设置drawable:

        val drawable = ContextCompat.getDrawable(context!!,R.drawable.ic_image)?.apply {
            setBounds(0, 0, intrinsicWidth, intrinsicHeight)
        }
        

        val drawable = ResourcesCompat.getDrawable(resources, R.drawable.ic_image, null)?.apply {
            setBounds(0, 0, minimumWidth, minimumHeight)
        }
        

        2) 设置TextView:

        textView.setCompoundDrawablesWithIntrinsicBounds(drawable, null, null, null)
        

        button.setCompoundDrawables(null, drawable, null, null)
        

        【讨论】:

        • 对于 Textview setCompoundDrawablesWithIntrinsicBounds 将起作用..
        【解决方案9】:

        【讨论】:

        • 有什么用?删除可绘制对象?
        【解决方案10】:

        Kotlin 示例:

            val myView = layoutInflater.inflate(R.layout.my_view, null) as TextView
            myView.setCompoundDrawablesWithIntrinsicBounds(0, myDrawable, 0, 0)
        

        【讨论】:

          猜你喜欢
          • 2021-08-14
          • 1970-01-01
          • 2013-10-07
          • 2012-07-02
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2019-02-04
          • 2014-11-05
          相关资源
          最近更新 更多