2016-02-26 67 views
5

我正在創建一個帶有用戶名和密碼字段的登錄窗口,並且在每個圖標中都想放置圖標。當我將圖標添加到EditText字段時,我無法更改圖標的大小。是否可以更改EditText中的圖標大小

圖標位於EditText字段內部,因爲我想在聚焦時更改EditText背景。

我試圖在LinearLayout中放置圖標和EditText視圖,但是當EditText被關注時,我無法更改佈局背景。

我的EditText代碼:

<EditText 
    android:id="@+id/user_name_edit_text" 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:layout_marginLeft="35dp" 
    android:layout_marginRight="35dp" 
    android:layout_marginTop="35dp" 
    android:background="@drawable/custom_border_selector" 
    android:drawablePadding="5dp" 
    android:hint="@string/username" 
    android:singleLine="true" 
    android:textColor="#757575" 
    android:textSize="15sp" 
    android:drawableLeft="@drawable/ic_person_grey_24dp" 
    /> 

的EditText視覺:

enter image description here

+0

檢查此鏈接:http://stackoverflow.com/questions/20250638/how-to-create-edittext-hint-as-text-with-image-in-android – Mrunal

+0

@ Rohit5k2它將聽起來很蠢我的,但我沒有想到,謝謝。還有一個問題,我找不到爲圖像添加填充或邊距的方法?我試圖谷歌,幾個小時,沒有有用的信息。 – JonasSeputis

+0

@JonasSeputis:請檢查。 – Rohit5k2

回答

5

您可以更改圖標和焦點使用較小的一個。這是你應該使用的方法。

public void setCompoundDrawablesWithIntrinsicBounds (
             int left, int top, int right, int bottom) 

設置可繪(如果有的話)顯示給的,上面的左邊,右邊,和文字的下方。如果您不想在此處繪製Drawable,請使用0。 Drawables的邊界將被設置爲它們的內在邊界。

我們添加填充您可以使用此

public void setCompoundDrawablePadding (int pad) 

設置複合可繪製與文本之間的填充的大小。

相關XML屬性:

android:drawablePadding 

更多信息herehere。還有許多其他方法可能會讓你感興趣。

10

如果你想解決XML中的問題,這裏的示例代碼,你可以操縱你的圖像尺寸/填充:

<!-- USERNAME INPUT --> 
<RelativeLayout 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:background="@drawable/edit_text_selector"> 

    <!-- INPUT --> 
    <EditText 
     android:id="@+id/username_input" 
     android:layout_marginLeft="-10dp" 
     android:layout_toRightOf="@+id/username_icon" 
     android:text="" 
     android:hint="Username" 
     android:padding="10dp" 
     android:background="" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content"/> 

    <!-- ICON --> 
    <ImageView 
     android:padding="3dp" 
     android:id="@+id/username_icon" 
     android:src="@drawable/perm_group_personal_info" 
     android:layout_width="40dp" 
     android:layout_height="40dp" /> 

</RelativeLayout> 

這裏是它的樣子:

enter image description here

1

比通過java設置更好的方法是通過如下設置它在一個分層drawable中。

Layered list drawable is as under : 

    <?xml version="1.0" encoding="utf-8"?> 
<layer-list xmlns:android="http://schemas.android.com/apk/res/android"> 
    <item 

     > 
     <shape> 
      <solid android:color="@color/bg_row_drop_completed"/> 
      <size android:width="96dp" 
       /> 
      <padding android:top="-15dp" 
       android:bottom="-15dp"/> 
      <corners android:topLeftRadius="@dimen/btn_add_bg_radius_purple" android:topRightRadius="@dimen/btn_add_bg_radius_purple"/> 
     </shape> 
    </item> 
    <item 
     > 
     <bitmap android:src="@drawable/_ic_arrow_drop_up_normal" android:gravity="center_vertical" 
      ></bitmap> 
    </item> 
</layer-list>[enter image description here][1] 
+0

它似乎不適用於API 19? – Sam

相關問題