【问题标题】:EditText Padding not Working as Expected in AndroidEditText 填充在 Android 中无法按预期工作
【发布时间】:2016-11-15 13:17:35
【问题描述】:

我在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);

输出:

注意:我已经在 Marshmallow、LolliPop 和 Jellybean 上进行了测试。但在所有设备不工作。

【问题讨论】:

  • 布局好像没问题。发布整个布局代码。
  • @AkshayBhat 你看到屏幕截图了吗?问题是显示..
  • 是的,但是我复制了你的布局代码并尝试过,它工作正常
  • @AkshayBhat 发布屏幕截图..

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


【解决方案1】:

您没有指定您设置的填充单位,因此默认情况下它采用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

【讨论】:

  • 很高兴知道快乐的编码..!! :)
  • 当我在此代码上设置paddingLeft 时,它只会显示PaddingBottom 而不是PaddingLeft。
  • 你是在第一次争论吗?? setPadding 的?确保在以编程方式设置填充时没有任何 xml 填充,否则它们会发生冲突..!!
  • Thanx 但是我发现我在我的 Xml 中做错了什么我设置了dip 所以它没有像我预期的那样显示但是当我将dip 更改为dp 时工作正常...
【解决方案2】:

尝试使用这个:

android:drawablePadding="8dp"

这个填充将把可绘制从文本光标中移开 看例子。

【讨论】:

    猜你喜欢
    • 2023-03-05
    • 2015-01-26
    • 1970-01-01
    • 2023-03-20
    • 1970-01-01
    • 2019-08-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多