Effect in EditText from xml

2019-07-11 04:37发布

问题:

I am trying to give shadow effect as shown below to edittext

but i am unable to do it. Below is what i have done till now

My code are as follow:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/background" >

 <RelativeLayout
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_centerInParent="true" >

    <LinearLayout
        android:id="@+id/layout_linear_login"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/img_6parcels"
        android:layout_centerInParent="true"
        android:layout_marginBottom="10dp"
        android:background="@drawable/screen_background"
        android:gravity="center"
        android:orientation="vertical"
        android:paddingBottom="20dp"
        android:paddingLeft="20dp"
        android:paddingRight="20dp"
        android:paddingTop="20dp" >

        <EditText
            android:id="@+id/edit_text_domain"
            android:layout_width="150dp"
            android:layout_height="wrap_content"
            android:gravity="center"
            android:hint="Domain"
            android:paddingBottom="3dp"
            android:background="@drawable/edittext_shadow" 
            android:paddingTop="3dp">
            <requestFocus />
        </EditText>

        <ImageView
            android:id="@+id/address"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="right"
            android:layout_marginBottom="3dp"
            android:layout_marginTop="3dp"
            android:src="@drawable/slice_address_transparent" />

        <EditText
            android:id="@+id/edit_text_invinzee"
            android:layout_width="150dp"
            android:layout_height="wrap_content"
            android:layout_marginTop="10dp"
            android:background="@drawable/edittext_shadow" 
            android:gravity="center"
            android:hint="Invinzee"
            android:paddingBottom="3dp"
            android:paddingTop="3dp" />

        <EditText
            android:id="@+id/edit_text_password"
            android:layout_width="150dp"
            android:layout_height="wrap_content"
            android:layout_marginTop="10dp"
            android:background="@drawable/edittext_shadow"
            android:gravity="center"
            android:hint="Password"
            android:inputType="textPassword"
            android:paddingBottom="3dp"
            android:paddingTop="3dp" />

        <Button
            android:id="@+id/log_in_Button"
            android:layout_width="100dp"
            android:layout_height="40dp"
            android:layout_gravity="center"
            android:layout_marginTop="25dp"
            android:gravity="center"
            android:text="LOGIN" />
    </LinearLayout>

    <LinearLayout
        android:id="@+id/img_6parcels"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
        android:layout_marginBottom="10dp"
        android:layout_marginTop="20dp" >

        <ImageView
            android:id="@+id/parcel"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_centerHorizontal="true"
            android:layout_gravity="center"
            android:layout_marginBottom="10dp"
            android:layout_marginTop="10dp"
            android:src="@drawable/six3_03" />
    </LinearLayout>

    <TextView
        android:id="@+id/text_delivery"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/layout_linear_login"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="10dp"
        android:text="Perfect Job"
        android:textColor="#353f41" />
</RelativeLayout>

</RelativeLayout>

My edittext_shadow.xml is as follow:

<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">

<!-- most important is order of layers -->

<!-- Bottom right side 2dp Shadow -->
<item >
    <shape android:shape="rectangle">
        <solid android:color="#660099" />           
    </shape>
</item>

<!-- Bottom 2dp Shadow -->
<item>
    <shape android:shape="rectangle">
        <solid android:color="#660099" />   
    </shape>
</item>

<!-- White Top color -->
<item android:top="-3px" android:left="-3px">
    <shape android:shape="rectangle">
        <solid android:color="#86ae0b" />  

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

I tried as shown in this link Add drop shadow effects to EditText Field but unable to accomplish it.

Any Help!!!

回答1:

Thanks Guys For helping.

Mixing code from Add drop shadow effects to EditText Field and answer above i was able to find the solution.

My solution is as below:

<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">

<!-- most important is order of layers -->

<!-- Bottom right side 2dp Shadow -->
<item >
    <shape android:shape="rectangle">
        <gradient
    android:angle="270"
    android:endColor="#333333"
     android:centerColor="#86ae0b"
    android:startColor="#666666" />          
    </shape>
</item>

<!-- Bottom 2dp Shadow -->
<item>
    <shape android:shape="rectangle">
        <gradient
    android:angle="270"
    android:endColor="#333333"
     android:centerColor="#86ae0b"
    android:startColor="#666666" />
    </shape>
</item>

<!-- White Top color -->
<item android:top="5px" android:left="5px">
    <shape android:shape="rectangle">
        <gradient
    android:angle="-90"
    android:endColor="#333333"
     android:centerColor="#86ae0b"
    android:startColor="#666666" />

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

Thanks alot for the help it means a lot to me.



回答2:

Why you didn't try gradient? If it is possible please let me know..

 <gradient
        android:angle="270"
        android:centerColor="@color/shadow_alpha"
        android:endColor="@color/shadow_alpha1"
        android:startColor="@color/shadow_alpha2" />

Define the colors as per your need..



回答3:

create an xml shape.xml put in drawable folders and give your edit text background

android:background="@drawable/shape"
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
      android:shape="rectangle" 
      android:padding="10dp">
    <!-- you can use any color you want I used here gray color-->
    <gradient android:angle="-90"
          android:endColor="#436EEE" 
          android:startColor="#436EEE"
          android:centerColor="#4876FF" />        
</shape>