Android 动态设置TextView的drawableLeft

有两种:

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

 

示例:

### 设置 `TextView` 中 `drawableLeft` 图标的大小和颜色 在 Android 开发中,若需调整 `TextView` 的 `drawableLeft` 图标的大小和颜色,可以通过 XML 属性结合代码实现。由于 `drawableLeft` 的默认行为无法直接通过 XML 设置其宽高,因此需要在代码中对 `Drawable` 进行尺寸调整,并通过 `setColorFilter` 方法修改其颜色。 #### 调整 `drawableLeft` 的大小 可以通过自定义 `TextView` 并在代码中对 `Drawable` 的宽高进行设置来实现尺寸调整。具体做法是获取 `TextView` 的 `drawableLeft`,判断其是否为空,然后根据设定的宽高进行缩放绘制 [^3]。 ```java public class CustomTextView extends AppCompatTextView { private int drawableWidth = -1; private int drawableHeight = -1; public CustomTextView(Context context) { super(context); } public CustomTextView(Context context, AttributeSet attrs) { super(context, attrs); TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.CustomTextView); drawableWidth = a.getDimensionPixelSize(R.styleable.CustomTextView_drawableWidth, -1); drawableHeight = a.getDimensionPixelSize(R.styleable.CustomTextView_drawableHeight, -1); a.recycle(); } @Override protected void onDraw(Canvas canvas) { Drawable[] drawables = getCompoundDrawables(); Drawable drawableLeft = drawables[0]; if (drawableLeft != null) { int textHeight = (int) getTextSize(); int drawableWidth = this.drawableWidth != -1 ? this.drawableWidth : textHeight; int drawableHeight = this.drawableHeight != -1 ? this.drawableHeight : textHeight; drawableLeft.setBounds(0, 0, drawableWidth, drawableHeight); drawables[0].setBounds(0, 0, drawableWidth, drawableHeight); setCompoundDrawables(drawables[0], drawables[1], drawables[2], drawables[3]); } super.onDraw(canvas); } } ``` 在 `res/values/attrs.xml` 中定义自定义属性: ```xml <declare-styleable name="CustomTextView"> <attr name="drawableWidth" format="dimension" /> <attr name="drawableHeight" format="dimension" /> </declare-styleable> ``` #### 修改 `drawableLeft` 的颜色 为了修改 `drawableLeft` 图标的颜色,可以在代码中使用 `setColorFilter` 方法。该方法适用于所有 `Drawable` 对象,可以动态改变其显示颜色 [^2]。 ```java Drawable drawableLeft = textView.getCompoundDrawables()[0]; if (drawableLeft != null) { drawableLeft.setColorFilter(Color.RED, PorterDuff.Mode.SRC_IN); } ``` 若需在 XML 中直接设置颜色,可以通过自定义 `TextView` 并在构造函数中读取颜色属性,然后对 `Drawable` 应用颜色过滤。 #### 实现文本与图标居中对齐 由于 `TextView` 的文本默认存在上下间距,导致 `drawableLeft` 图标与文本无法垂直居中。可以通过调整 `TextView` 的 `gravity` 属性,并结合 `setBounds()` 方法设置 `Drawable` 的绘制区域,以实现视觉上的垂直居中对齐 [^1]。 ```java textView.setGravity(Gravity.CENTER_VERTICAL); ``` 在 XML 中: ```xml <TextView android:layout_width="wrap_content" android:layout_height="48dp" android:gravity="center_vertical" android:drawableLeft="@drawable/icon" /> ``` --- ###
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值