android 设置textview 左边,Android设置图片在TextView上、下、左、右

本文详细介绍了在Android中如何通过布局文件和代码动态设置TextView的图文混排,包括`android:drawablePadding`属性的使用以及`setCompoundDrawables`和`setCompoundDrawablesWithIntrinsicBounds`的区别。重点讲解了这两个方法在设置Drawable显示位置时的不同,并强调了在使用前需先设置Drawable的边界条件。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

一种是在布局文件中的设置

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()获得

14b86738ed99?utm_campaign=maleskine&utm_content=note&utm_medium=seo_notes&utm_source=recommendation

欺负我腿短.gif

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值