Android设置图片在TextView上、下、左、右
2018-11-22 本文已影响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()获得