简体   繁体   中英

TextInputLayout change color to gradient

Is there a way to change any of the TextInputLayout's color to have a gradient. The answers I've seen so far allow the normal, activated, and highlighted colors to be changed to a single color through XML.

Yes you just need to create an XML shape file eg drawable/mygradient.xml :

<?xml version="1.0" encoding="UTF-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle" >

    <gradient
        android:angle="90"
        android:centerColor="#555994"
        android:endColor="#b5b6d2"
        android:startColor="#555994"
        android:type="linear" />

    <corners
        android:radius="0dp"/>

</shape>

and set TextInputLayout 's background as android:background="@drawable/mygradient"

Add this as background @drawable/gradient:

<layer-list xmlns:android="http://schemas.android.com/apk/res/android" >

    <item>
        <!-- create gradient you want to use with the angle you want to use -->
        <shape android:shape="rectangle" >
            <gradient
                android:angle="0"
                android:centerColor="@android:color/holo_blue_bright"
                android:endColor="@android:color/holo_red_light"
                android:startColor="@android:color/holo_green_light" />

        </shape>
    </item>
    <!-- create the stroke for top, left, bottom and right with the dp you want -->
    <item
        android:bottom="2dp"
        android:left="2dp"
        android:right="2dp"
        android:top="2dp">
        <shape android:shape="rectangle" >
            <!-- fill the inside in the color you want (could also be a gradient again if you want to, just change solid to gradient and enter angle, start, maybe center, and end color) -->
            <solid android:color="#fff" />
        </shape>
    </item>

</layer-list>

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