TextView,Button的DrawableLeft,,Right 使用代码动态的设置

Xml文件中设置相信大家都已经非常的熟悉了.

            <TextView
                android:id="@+id/register_zone"
                android:layout_width="wrap_content"
                android:layout_height="match_parent"
                android:drawableRight="@mipmap/lbs_arrow_down"/>

使用代码设置DrawableLeft,Right

第一种方案

       Drawable drawable = SwmUIUtils.getDrawable(imgs[position]);
                    // 确定drawable的位置
                    drawable.setBounds(0, 0, drawable.getMinimumWidth(), drawable.getMinimumHeight());
//                      setCompoundDrawables 画的drawable的宽高是按drawable.setBound()设置的宽高,
//                    所以才有The Drawables must already have had setBounds(Rect) called.
//                    使用之前必须使用Drawable.setBounds设置Drawable的长宽。`

                    mTaskProgress.setCompoundDrawables(null, null, drawable, null);

第二种方案

setCompoundDrawablesWithIntrinsicBounds(null, null, UIUtils.getDrawable(R.mipmap.up_arrow), null);

setCompoundDrawablesWithIntrinsicBounds是画的drawable的宽高是按drawable固定的宽高,
所以才有The Drawables' bounds will be set to their intrinsic bounds.

即通过getIntrinsicWidth()与getIntrinsicHeight()获得,
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 一. Java基础部分.................................................
    wy_sure阅读 9,255评论 0 11
  • 1. Java基础部分 基础部分的顺序:基本语法,类相关的语法,内部类的语法,继承相关的语法,异常的语法,线程的语...
    子非鱼_t_阅读 31,955评论 18 399
  • Android 自定义View的各种姿势1 Activity的显示之ViewRootImpl详解 Activity...
    passiontim阅读 175,641评论 25 709
  • 217. Contains Duplicate 这是leetCode第217题 题目 Given an array...
    打铁大师阅读 2,206评论 0 2
  • 漫漫人生路 ,已经二十一的人了,现在大三的生活甚是迷茫,马上面临毕业,我将何去何从,又不甘心碌碌无为,更不甘心平庸...
    懂得怜悯阅读 2,498评论 0 0