简体   繁体   中英

Android EditText color change destroys margins

I have a set of edittexts in a relative layout displayed in a vertical line. By default they have a tiny margin between them although I don't have any margin set. When I change the background color of one of them its bottom margin is lost and the space is shortened to what I assume is the tiny top margin of the edittext below it. I tried to use setPadding() , but with no result. The only thing that worked was setBackground() , but that also changes the color to the original one.

Instead of setPadding try setting margin in xml to the Edittexts. Incase you need to set margin by code you will have to modify layout params of the edit texts.

Setting margin in XML should solve your problem.

This works for me:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent"
android:alpha="0.9">

<EditText
    android:id="@+id/editText1"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_alignParentLeft="true"
    android:layout_alignParentTop="true"
    android:background="#8A084B"
    android:ems="10" />

<EditText
    android:id="@+id/editText2"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_alignParentLeft="true"
    android:layout_below="@+id/editText1"
    android:background="@android:color/black"
    android:ems="10" >

    <requestFocus />
</EditText>

<EditText
    android:id="@+id/editText3"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_alignParentLeft="true"
    android:layout_below="@+id/editText2"
    android:background="@android:color/darker_gray"
    android:ems="10" />

And with some margins between them:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent"
android:alpha="0.9">

<EditText
    android:id="@+id/editText1"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_alignParentLeft="true"
    android:layout_alignParentTop="true"
    android:layout_marginBottom="2dp"
    android:layout_marginTop="2dp"
    android:background="#8A084B"
    android:ems="10" />

<EditText
    android:id="@+id/editText2"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_alignParentLeft="true"
    android:layout_below="@+id/editText1"
    android:layout_marginBottom="2dp"
    android:background="@android:color/black"
    android:ems="10" >

    <requestFocus />
</EditText>

<EditText
    android:id="@+id/editText3"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_alignParentLeft="true"
    android:layout_below="@+id/editText2"
    android:background="@android:color/darker_gray"
    android:ems="10" />

If you want, you can fix them with the id's and android:layout_alignTop= or Bottom. I hope it helps.

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