我在 MarshMallow
设备上进行了测试。当我为 EditText
编写 xml 并将 PaddingBottom
设置为 Retype EditText
时。但它不工作。但是当我将 PaddingLeft
设置为 New PassWord
时,EditText
工作正常。
方法:1
xml代码:
<EditText
android:id="@+id/change_password_old_password_edittext"
android:layout_below="@+id/change_password_mobileNumber_text"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Old Password"
android:layout_marginTop="20.0dip"
android:textColorHint="#40000000"
android:textSize="@dimen/textsize16"/>
<EditText
android:id="@+id/change_password_new_password_edittext"
android:layout_below="@+id/change_password_old_password_edittext"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="New Password"
android:layout_marginTop="20.0dip"
android:textColorHint="#40000000"
android:paddingLeft="10dip"
android:textSize="@dimen/textsize16"/>
<EditText
android:id="@+id/change_password_retype_password_edittext"
android:layout_below="@+id/change_password_new_password_edittext"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Retype Password"
android:layout_marginTop="20.0dip"
android:paddingBottom="10dip"
android:textColorHint="#40000000"
android:textSize="@dimen/textsize16"/>
截图:
方法:2
然后我决定以编程方式设置 PaddingBottom
。但它给出了奇怪的输出。
Java 代码:
在 OnCreate
方法中
EditText change_password_retype_password_edittext = (EditText)findViewById(R.id.change_password_retype_password_edittext);
change_password_retype_password_edittext.setPadding(0,0,0,10);
输出:
Note : I have tested on
Marshmallow
,LolliPop
and alsoJellybean
. But in all device is not Working.
最佳答案
您没有指定您设置的填充单位,因此它默认采用 pixel
,它小于 dp
,因此结果不符合您的预期。
将像素转换为 dp 你会得到解决方案:
int paddingPixel = 10;
float density = getResources().getDisplayMetrics().density;
int paddingDp = (int)(paddingPixel * density);
change_password_retype_password_edittext.setPadding(0,0,0,paddingDp);
关于android - EditText 填充在 Android 中无法正常工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38343421/