2016-07-13 25 views
0

我已在MarshMallow設備上進行測試。當我爲EditText寫入xml並將PaddingBottom設置爲Retype EditText。但它不工作。但是當我設置PaddingLeftNew PassWordEditText工作正常。EditText填充不按預期在Android中工作

計算策略: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

備註:我已在MarshmallowLolliPopJellybean上進行過測試。但在所有設備不工作。

+0

這似乎與佈局沒有問題。發佈整個佈局代碼。 –

+0

@AkshayBhat你看到屏幕截圖的問題是顯示.. – Ironman

+0

是的,但我複製你的佈局代碼,並嘗試,它工作正常 –

回答

3

你沒有指定填充用戶所設置的它正在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

+0

很酷它的工作... – Ironman

+1

很高興知道快樂編碼..! :) –

+0

當我在此代碼上設置'paddingLeft'時,只有它顯示'PaddingBottom'而不是'PaddingLeft'。 – Ironman