我正在创建一个包含用户名和密码字段的登录窗口,每个我都想放置图标.当我向EditText字段添加图标时,我无法更改图标的大小.
图标在EditText字段内,因为我想在聚焦时更改EditText背景.
我试图将图标和EditText视图放在LinearLayout中,但是当EditText聚焦时我无法更改布局背景.
My EditText code:
<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 visual:
解决方法
You can change the icon and use a smaller one on focus. This is the method you should use.
public void setCompoundDrawablesWithIntrinsicBounds (
int left,int top,int right,int bottom)
将Drawables(如果有)设置为显示在文本的左侧,上方,右侧和下方.如果你不想在那里使用Drawable,请使用0. Drawables的界限将设置为其内在界限.
Now to add padding you can use this
public void setCompoundDrawablePadding (int pad)
设置复合drawable和文本之间填充的大小.
相关的XML属性:
android:drawablePadding
更多信息here和here.您可能会发现许多其他有趣的方法.