如何在android中实现相反的渐变?

时间:2015-04-30 07:17:14

标签: android gradient xml-drawable

我正在使用此

<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item>
    <shape android:shape="rectangle">
        <corners android:radius="7sp" />
        <gradient
            android:startColor="#333"
            android:centerColor="#ffffff"
            android:endColor="#333"
            android:angle="90" />

            <stroke android:width="1dp" android:color="#FFffffff" />
    </shape>
</item>

但这并没有产生预期的效果。

enter image description here

4 个答案:

答案 0 :(得分:2)

你可以这样试试

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

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="50dp"
        android:layout_alignParentBottom="true"
        android:layout_alignParentLeft="true"
        android:background="@drawable/gradient"
        android:orientation="vertical" >
    </RelativeLayout>

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="50dp"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true"
        android:background="@drawable/gradient2"
        android:orientation="vertical" >
    </RelativeLayout>

</RelativeLayout>

渐变1

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

    <item>
        <shape android:shape="rectangle" >
            <corners android:radius="7sp" />

            <gradient
                android:angle="90"
                android:endColor="#D1D1D1"
                android:startColor="#000" />

            <stroke
                android:width="1dp"
                android:color="#FFffffff" />
        </shape>
    </item>

</layer-list>

Gradient 2

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

    <item>
        <shape android:shape="rectangle" >
            <corners android:radius="7sp" />

            <gradient
                android:angle="90"
                android:endColor="#000"
                android:startColor="#D1D1D1" />

            <stroke
                android:width="1dp"
                android:color="#FFffffff" />
        </shape>
    </item>

</layer-list>

最终结果

enter image description here

答案 1 :(得分:1)

改为使用

<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
    <item>
        <shape android:shape="rectangle">
            <gradient
                android:startColor="#333"
                android:centerColor="#DDD"
                android:endColor="#333"
                android:angle="90" />

                <stroke android:width="1dp" android:color="#FF333333" />
        </shape>
    </item>
    <item android:left="4dp" android:right="4dp" android:top="1dp" android:bottom="1dp">
        <shape android:shape="rectangle">
            <gradient
                android:startColor="#AAA"
                android:centerColor="#FFF"
                android:endColor="#AAA"
                android:angle="90" />
        </shape>
    </item>
</layer-list>

但最好使用9个补丁图像

答案 2 :(得分:1)

尝试

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item>
    <shape>
        <gradient
            android:startColor="#4C4C43"
            android:centerColor="#B8B894"
            android:endColor="#4C4C43"
            android:angle="270" />
    </shape>
</item>

enter image description here

P.S - 根据您的需要改变颜色

reference- http://www.learn-android-easily.com/2013/06/gradient-drawable-in-android.html

答案 3 :(得分:0)

使用:

<shape xmlns:android="http://schemas.android.com/apk/res/android"  android:shape="rectangle" >
 <gradient 
   android:type="linear"
   android:centerX="50%" 
   android:startColor="#FFc0c0c0" 
   android:centerColor="#FFffffff" 
   android:endColor="#FF808080" 
   android:angle="270"/>
</shape>

<corners android:radius="7sp" />