【问题标题】:Is it possible to change the size of icon inside EditText是否可以更改 EditText 内的图标大小
【发布时间】:2016-06-09 14:04:51
【问题描述】:

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

图标在 EditText 字段内,因为我想在它获得焦点时更改 EditText 背景。

我尝试将图标和 Ed​​itText 视图放在 LinearLayout 中,但是当 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 视觉效果:

【问题讨论】:

  • @Rohit5k2 这听起来很愚蠢,但我没有想到,谢谢。还有一个问题,我找不到为图像添加填充或边距的方法?我试着用谷歌搜索了几个小时,没有有用的信息。
  • @JonasSeputis:请检查。
  • @Rohit5k2 你的回答很有帮助,谢谢!

标签: android android-layout android-studio


【解决方案1】:

如果您想在 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>

下面是它的样子:

【讨论】:

    【解决方案2】:

    您可以更改图标并在焦点上使用较小的图标。这是你应该使用的方法。

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

    将可绘制对象(如果有)设置为显示在文本的左侧、上方、右侧和下方。如果您不想要 Drawable,请使用 0。 Drawable 的边界将设置为其固有边界。

    现在要添加填充,您可以使用它

     public void setCompoundDrawablePadding (int pad)
    

    设置复合可绘制对象和文本之间的填充大小。

    相关的 XML 属性:

    android:drawablePadding
    

    更多信息herehere。您可能会发现许多其他有趣的方法。

    【讨论】:

      【解决方案3】:

      比通过 java 设置更好的方法是将 It 设置在分层可绘制对象中,如下所示。

      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>
      

      see example image

      【讨论】:

        猜你喜欢
        • 2021-11-26
        • 2018-04-17
        • 1970-01-01
        • 2015-10-20
        • 2022-09-23
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2020-11-02
        相关资源
        最近更新 更多