简体   繁体   中英

Relativelayout turns grey when setting drawable to Edittext background

I created a drawable for EditText and set it as background( @drawable/edttxt_bg2 ) to the EditText. When I set the drawable to the view the parent RelativeLayout turns grey in the design preview. I tried cleaning and rebuilding the project and also invalidating caches. After doing that it displays correctly. Whenever I make changes to the views it changes the RelativeLayout color changes back to grey.

upload_lyt.xml:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#ffffff"
    xmlns:app="http://schemas.android.com/apk/res-auto"
>
<RelativeLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="@drawable/edt_txt_bg"
    android:layout_marginTop="@dimen/rgstr_mrgn_top"
    android:layout_marginLeft="@dimen/rgstr_mrgn_sides"
    android:layout_marginRight="@dimen/rgstr_mrgn_sides">
<TextView
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:textSize="20sp"
    android:id="@+id/upload_lbl"
    android:textColor="#ffffff"
    android:background="#1565C0"
    android:text="ENTER THE PAGE DETAILS"
    android:gravity="center"
    />
    <EditText
        android:layout_width="match_parent"
        android:layout_height="@dimen/rgstr_edtxt_hght"
        android:layout_below="@+id/upload_lbl"
        android:background="@drawable/edttxt_bg2"
        android:id="@+id/fb_uri"
        android:gravity="center"
        android:inputType="textPersonName"
        android:layout_marginTop="@dimen/upld_edttxt_mrgn_top"/>
    <EditText
        android:layout_width="match_parent"
        android:layout_height="@dimen/rgstr_edtxt_hght"
        android:layout_below="@+id/fb_uri"
        android:background="@drawable/edttxt_bg2"
        android:id="@+id/price"

        android:gravity="center"
        android:inputType="numberDecimal"
        />
    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_below="@+id/price"
        android:id="@+id/qstnlbl"
        android:gravity="center"
        android:textSize="20sp"
        android:layout_marginTop="@dimen/rgstr_edttxt_mrgn_top"
        android:text="Where is the page mostly targetted? Care to specify location?"
        />
    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_below="@+id/qstnlbl"
        android:textSize="20sp"
        android:id="@+id/neg_lbl"
        android:text="Negotiable?"
        android:gravity="center"
        android:layout_marginTop="@dimen/rgstr_edttxt_mrgn_top"/>
    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/neg_lyt"
        android:layout_marginTop="@dimen/neg_mrgn_top"
        android:layout_below="@+id/neg_lbl">
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textSize="20sp"
            android:id="@+id/no"
            android:text="No"
            android:layout_centerVertical="true"
            android:layout_marginStart="@dimen/neg_mrgn_sds"

            />
        <com.suke.widget.SwitchButton
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_centerVertical="true"
            android:id="@+id/switch1"
            app:sb_uncheckcircle_width="0dp"
            app:sb_uncheckcircle_color="@android:color/transparent"
            app:sb_checkline_width="0dp"
            app:sb_button_color="#1565C0"
            app:sb_checkline_color="@android:color/transparent"
            app:sb_uncheck_color="#1565C0"
            app:sb_checked_color="#1565C0"
            android:layout_centerHorizontal="true"
            />
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/yes"
            android:text="Yes"
            android:textColor="#1565C0"
            android:textSize="20sp"
            android:layout_alignParentEnd="true"
            android:layout_centerVertical="true"
            android:layout_marginEnd="@dimen/neg_mrgn_sds">

        </TextView>

    </RelativeLayout>
    <Button
        android:layout_below="@+id/neg_lyt"
        android:layout_marginTop="@dimen/rgstr_btn_top"
        android:background="@drawable/otp_btn"
        android:textColor="#ffffff"
        android:text="UPLOAD"
        android:id="@+id/upload"
        android:layout_marginBottom="30dp"
        android:layout_marginRight="@dimen/rgstr_btn_mrgn_sides"
        android:layout_marginLeft="@dimen/rgstr_btn_mrgn_sides"
        android:layout_width="match_parent"
        android:layout_height="@dimen/rgstr_btn_hght"
        android:layout_centerHorizontal="true"/>

</RelativeLayout>
</RelativeLayout>

@drawable/edttxt_bg2:

<?xml version="1.0" encoding="utf-8"?>
<shape 
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="rectangle">
    <solid 
        android:color="#FAFAFA">
    </solid>
    <stroke 
        android:color="#1565C0"
        android:width="0.5dp">
    </stroke>
</shape>

@drawable/edt_txt_bg

<?xml version="1.0" encoding="utf-8"?>
<layer-list>
<item
    xmlns:android="http://schemas.android.com/apk/res/android"
  android:top="-2dp"
  android:bottom="2dp"
    android:left ="-2dp"
    android:right="-2dp">

<shape
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="rectangle">
<solid
    android:color="#ffffff"></solid>

    <stroke
        android:color="#1565C0"
        android:width="0.5dp">

    </stroke>
</shape>
</item>
</layer-list>

Layout Picture: 在此处输入图片说明

remove these lines from @drawable/edt_txt_bg.It will work. You have not defined any fixed dimension for stroke. That's the main reason.

<stroke
    android:color="#1565C0"
    android:width="0.5dp">

</stroke>

Don't force to change the Theme in layout xml, implement new style in styles.xml if you want to use custom actionbar.

<style name="CustomTheme" parent="Theme.AppCompat.Light.NoActionBar">
    <item name="contentInsetStart">0dp</item>
    <!--<item name="contentInsetEnd">0dp</item>-->
    <item name="android:windowContentOverlay">@null</item>
    <item name="android:homeAsUpIndicator">@null</item>
</style>

Then in AndroidManifest.xml , set this theme to your activity or application tag.

android:theme="@style/CustomTheme"

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