调整EditText高度后提示文本(hint)不显示的问题求助
解决EditText调整高度后Hint不显示的问题
嘿,这个问题我太熟了!你遇到的是EditText高度设置过小导致hint文本被裁剪的问题,我给你几个实用的解决方案:
核心原因
你把EditText的layout_height设成了20dp,但你的textSize就有16dp了,EditText本身默认还有上下内边距(padding),这么一来hint文本根本没有足够的空间显示,直接被裁掉了。
解决方案
1. 调高EditText的高度(最简单直接)
给EditText设置一个足够容纳文本和内边距的高度,比如改成40dp,这样hint就能正常显示了:
<EditText android:id="@+id/editText" android:layout_width="200dp" android:layout_height="40dp" <!-- 调整为足够的高度 --> android:layout_below="@+id/imageView" android:layout_centerHorizontal="true" android:layout_marginTop="69dp" android:drawableLeft="@drawable/ic_action_user" android:ems="10" android:hint="Name" android:inputType="textPersonName" android:textSize="16dp"/>
2. 自定义内边距(保留20dp高度的前提下)
如果一定要用20dp的高度,可以手动缩小上下内边距,给文本腾出空间。注意别把内边距设为0,不然文本会贴边影响美观:
<EditText android:id="@+id/editText" android:layout_width="200dp" android:layout_height="20dp" android:layout_below="@+id/imageView" android:layout_centerHorizontal="true" android:layout_marginTop="69dp" android:drawableLeft="@drawable/ic_action_user" android:ems="10" android:hint="Name" android:inputType="textPersonName" android:textSize="16dp" android:paddingTop="2dp" <!-- 自定义上下内边距 --> android:paddingBottom="2dp"/>
3. 使用minHeight代替layout_height(更灵活的方案)
把layout_height改成wrap_content,同时设置minHeight为20dp,这样EditText会根据内容自动调整高度,但最小保持20dp,既满足高度要求又不会裁剪文本:
<EditText android:id="@+id/editText" android:layout_width="200dp" android:layout_height="wrap_content" android:minHeight="20dp" <!-- 设置最小高度 --> android:layout_below="@+id/imageView" android:layout_centerHorizontal="true" android:layout_marginTop="69dp" android:drawableLeft="@drawable/ic_action_user" android:ems="10" android:hint="Name" android:inputType="textPersonName" android:textSize="16dp"/>
内容的提问来源于stack exchange,提问作者Ahsan Azwar
相关产品推荐
相关产品推荐

