You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何调整TextInputEditText中Drawable的大小以匹配文本尺寸

问题描述

我有一个用于输入数字的TextInputEditText控件,已成功在其末尾添加了Drawable图标,但目前文本与Drawable图标尺寸不一致,希望将Drawable调整至与文本相同大小,该如何实现?

当前XML代码

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="@color/green"
    android:orientation="horizontal"
    android:weightSum="2"
    android:padding="10dp">
    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="amount"
        android:layout_weight="1"
        android:gravity="left"
        android:textColor="@color/white"
        android:textSize="15dp"/>
    <com.google.android.material.textfield.TextInputEditText
        android:id="@+id/amount_input"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:inputType="numberDecimal"
        android:textAlignment="textEnd"
        android:background="@color/transparent"
        android:textSize="15dp"
        android:drawableEnd="@drawable/ic_percent"/>
 </LinearLayout>

当前显示效果

输入框右侧的百分比图标尺寸明显大于文本字号,两者视觉比例不协调。


解决方案

方法1:XML属性直接设置Drawable尺寸

在TextInputEditText中添加android:drawableWidth和android:drawableHeight属性,将其值设为与文本大小一致(这里文本是15dp),同时可通过android:drawablePadding调整图标与文本的间距:

<com.google.android.material.textfield.TextInputEditText
    android:id="@+id/amount_input"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_weight="1"
    android:inputType="numberDecimal"
    android:textAlignment="textEnd"
    android:background="@color/transparent"
    android:textSize="15dp"
    android:drawableEnd="@drawable/ic_percent"
    android:drawableWidth="15dp"
    android:drawableHeight="15dp"
    android:drawablePadding="4dp"/>

方法2:代码动态适配文本大小

如果需要根据文本字号动态调整图标尺寸,可在代码中获取文本像素大小后缩放Drawable:

val amountInput = findViewById<TextInputEditText>(R.id.amount_input)
val textPixelSize = amountInput.textSize // 获取文本的像素尺寸
val endDrawable = amountInput.compoundDrawablesRelative[2] // 对应drawableEnd(索引:0左、1上、2右、3下)

endDrawable?.let {
    it.setBounds(0, 0, textPixelSize.toInt(), textPixelSize.toInt())
    amountInput.setCompoundDrawablesRelative(null, null, it, null)
}

方法3:修改矢量图的默认尺寸(适用于VectorDrawable)

若ic_percent是矢量图标,可直接在其XML文件中设置android:width和android:height为目标字号(15dp),确保图标默认尺寸与文本匹配:

<vector xmlns:android="http://schemas.android.com/apk/res/android"
    android:width="15dp"
    android:height="15dp"
    android:viewportWidth="24"
    android:viewportHeight="24">
    <path
        android:fillColor="@color/white"
        android:pathData="M12 2C6.48 2 2 6.48 2 12s4.48 10 10 10 10-4.48 10-10S17.52 2 12 2zm1 17h-2v-2h2v2zm2.07-7.75l-.9.92C13.45 12.9 13 13.5 13 15h-2v-.5c0-1.1.45-2.1 1.17-2.83l1.24-1.26c.37-.36.59-.86.59-1.41 0-1.1-.9-2-2-2s-2 .9-2 2H8c0-2.21 1.79-4 4-4s4 1.79 4 4c0 .88-.36 1.68-.93 2.25z"/>
</vector>

内容的提问来源于stack exchange,提问作者Sasuke

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.23 04:06:24