简体   繁体   中英

Android EditText - text move when showing top icon

I am showing icons on EditText with method setCompoundDrawablesWithIntrinsicBounds() , situation is following:

在此处输入图片说明

When I show top icon, text jumps / move down.

在此处输入图片说明

I set paddingTop & marginTop of editText as 0dp, but did not help. any ideas ?

<EditText android:id="@+id/editText_Search" android:layout_width="fill_parent" android:layout_height="80dp" android:background="@drawable/shadow" android:drawablePadding="10dp" android:ems="10" android:gravity="center" android:hint="@string/string_EditText_Hint_EnterFieldNumber" android:inputType="textNoSuggestions|textVisiblePassword" android:paddingTop="0dp" android:layout_marginTop="0dp" android:paddingLeft="12dp" android:paddingRight="12dp" android:textColor="@color/ForestGreen" android:textColorHint="@color/Silver" android:textColorLink="@color/White" android:textCursorDrawable="@drawable/black_cursor" > </EditText>

you are setting the height of the EditText to constant thus you do not let it expand after adding the top compound.

it will not matter though because you will still need to adjust the bottom container. if you know the height of the the Drawable , just set bottomPadding to be equal (programmatic).

you can also cheat and set a drawable as bottom one with alpha 0 or empty one

I would just use a different View.

<ImageView
    android:layout_height="20dp"
    android:src="@drawable/icon"
    android:visibility="invisible"/>
<EditText
    android:marginBottom="20dp"/>

assuming 20dp is your icon height. This way the EditText content will always stay centered in the box, and you can show/hide the icon by calling imageView.setVisibility(View.VISIBLE) or imageView.setVisibility(View.INVISIBLE) .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM