美文网首页
Android设置图片在TextView上、下、左、右

Android设置图片在TextView上、下、左、右

作者: 喜爱滴小麦芽 | 来源:发表于2018-11-22 22:54 被阅读15次
一种是在布局文件中的设置
android:drawablePadding="5dp"//图片与文案距离
android:drawableBottom="@drawable/icon_search"
android:drawableLeft="@drawable/icon_search"
android:drawableTop="@drawable/icon_search"
android:drawableRight="@drawable/icon_search"

但有时我们根据一些条件来设置显示的位置,这就要用到第二种动态代码设置

setCompoundDrawables(left, top, right, bottom);

setCompoundDrawablesWithIntrinsicBounds(left, top, right, bottom);

设置Drawable显示在text的左、上、右、下位置

两者区别:
setCompoundDrawables 画的drawable的宽高是按drawable.setBound()设置的宽高,所以才有The Drawables must already have had setBounds(Rect) called.
使用之前必须使用Drawable.setBounds设置Drawable的长宽。

setCompoundDrawablesWithIntrinsicBounds是画的drawable的宽高是按drawable固定的宽高,所以才有The Drawables' bounds will be set to their intrinsic bounds.
即通过getIntrinsicWidth()与getIntrinsicHeight()获得

欺负我腿短.gif

相关文章

网友评论

      本文标题:Android设置图片在TextView上、下、左、右

      本文链接:https://www.haomeiwen.com/subject/zvorqqtx.html