android - EditText 填充在 Android 中无法正常工作

标签 android xml android-layout android-studio android-edittext

我在 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"/>

截图:

enter image description here

方法: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);

输出:

enter image description here

Note : I have tested on Marshmallow , LolliPop and also Jellybean. 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);

更多信息请访问:Add padding on view programmatically

关于android - EditText 填充在 Android 中无法正常工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38343421/

相关文章:

android - 从字符串更改android textview颜色

android - 在 Android XML 中重用 TextView 代码

android - ScrollView 自动滚动到可选择的 TextView

android - 清晰的图标 清晰的 Android 应用程序

Android Groovy 网络服务

android - 如何退出谷歌登录?

c++ - RapidXML是否支持xml :space ="preserve"?

android - 一次关闭所有最近打开/运行的应用程序按钮-android

php - Android JSON 解析 | PHP 脚本和验证 JSON

java - Android - 将 fragment 添加到 xml 布局时出错