TextView设置图片

之前写过一篇关于TextView的文章Android采坑之TextView实际高度比TextSize大

今天再来写一篇关于TextView的文章,介绍TextView设置图片。
TextView设置图片有两种方式:xml、代码
举例:我们要在TextView的右边显示一张图片,可以这样做:

android:drawableRight="@mipmap/down_arrow_2"

也可以这样做:

tv_play_type.setCompoundDrawablesWithIntrinsicBounds(null, null, getResources().getDrawable(R.mipmap.down_arrow_2), null);

那么我们如何设置图片与文本之间的距离呢,同样有两种方式:xml、代码
我们仍然以在TextView的右边显示一张图片举例,可以这样做:

android:drawablePadding="5dp"

也可以这样做:

tv_play_type.setCompoundDrawablePadding(20);

OK,我们再说一下下面两个方法的区别:

setCompoundDrawablesWithIntrinsicBounds(@Nullable Drawable left,
            @Nullable Drawable top, @Nullable Drawable right, @Nullable Drawable bottom)
setCompoundDrawables(@Nullable Drawable left, @Nullable Drawable top,
            @Nullable Drawable right, @Nullable Drawable bottom)

第一个方法,画的drawable的宽高是按drawable固定的宽高,即通过getIntrinsicWidth()与getIntrinsicHeight()获得的。
第二个方法,画的drawable的宽高是按drawable.setBounds()设置的宽高,使用之前必须使用Drawable.setBounds设置Drawable的宽高。

猜你喜欢

转载自blog.csdn.net/zdj_Develop/article/details/87629619