2016-02-26 128 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

相关问题