本文介绍了是否可以更改 EditText 中图标的大小的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在创建一个带有用户名和密码字段的登录窗口,我想在每个窗口中放置图标.当我将图标添加到 EditText 字段时,我无法更改图标的大小.

图标位于 EditText 字段内,因为我想在聚焦时更改 EditText 背景.

我尝试将图标和 Ed​​itText 视图放在 LinearLayout 中,但是当 EditText 聚焦时我无法更改布局背景.

我的 EditText 代码:

EditText 视觉效果:

解决方案
public void setCompoundDrawablesWithIntrinsicBounds (int left, int top, int right, int bottom)

将可绘制对象(如果有)设置为出现在文本的左侧、上方、右侧和下方.如果您不想在那里使用 Drawable,请使用 0.可绘制对象的边界将设置为其内在边界.

现在添加填充你可以使用这个

 public void setCompoundDrawablePadding (int pad)

设置复合绘图和文本之间的填充大小.

相关 XML 属性:

android:drawablePadding

更多信息 此处此处.您可能会发现许多其他方法很有趣.

I am creating a login window with username and password fields and in each I would like to put icons. When I add icon to EditText field I cannot change the size of icon.

Icon is inside EditText field because I want to change EditText background when it is focused.

I tried to put icon and EditText view inside LinearLayout, but I couldn't change layout background when EditText is focused.

<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"
    />
解决方案
public void setCompoundDrawablesWithIntrinsicBounds (
                                       int left, int top, int right, int bottom)

Sets the Drawables (if any) to appear to the left of, above, to the right of, and below the text. Use 0 if you do not want a Drawable there. The Drawables' bounds will be set to their intrinsic bounds.

 public void setCompoundDrawablePadding (int pad)

Sets the size of the padding between the compound drawables and the text.

Related XML Attributes:

android:drawablePadding

More information here and here. There are many other methods you might find interesting.

这篇关于是否可以更改 EditText 中图标的大小的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-31 02:37