TextInputLayout将颜色更改为渐变

时间:2018-03-13 14:45:43

标签: android xml colors gradient android-textinputlayout

有没有办法可以更改任何TextInputLayout的颜色以获得渐变。到目前为止我看到的答案允许通过XML将正常,激活和突出显示的颜色更改为单一颜色。

2 个答案:

答案 0 :(得分:0)

是的,您只需要创建一个XML形状文件,例如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>

并将TextInputLayout的背景设为android:background="@drawable/mygradient"

答案 1 :(得分:0)

将此添加为背景@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>