web-dev-qa-db-fra.com

Android bordure de forme avec dégradé

Je veux créer une bordure pour une mise en page linéaire. Je décide donc de créer une forme. Je veux que la bordure ait un dégradé. Ce qui suit ne le fait pas. Il remplit le rectangle puis crée un trait. Je ne veux pas d'un rectangle plein, juste le trait. Et je veux appliquer le dégradé au trait.

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

    <gradient
        Android:angle="360"
        Android:centerColor="#e95a22"
        Android:endColor="#ff00b5"
        Android:gradientRadius="360"
        Android:startColor="#006386"
        Android:type="sweep" />

    <stroke
        Android:width="2dp"
        Android:color="#ff207d94" />

</shape>
30
learner

essayez quelque chose comme ceci:

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

    <item>
        <shape Android:shape="rectangle" >
            <gradient
                Android:angle="360"
                Android:centerColor="#e95a22"
                Android:endColor="#ff00b5"
                Android:gradientRadius="360"
                Android:startColor="#006386"
                Android:type="sweep" />

            <stroke
                Android:width="2dp"
                Android:color="#ff207d94" />
        </shape>
    </item>
    <item
        Android:bottom="2dp"
        Android:left="2dp"
        Android:right="2dp"
        Android:top="2dp">
        <shape Android:shape="rectangle" >
            <solid Android:color="#fff" />
        </shape>
    </item>

</layer-list>
36
vipul mittal

comme la réponse acceptée n'a pas fonctionné exactement comme je voulais qu'elle fonctionne pour moi, je posterai également ma solution, peut-être que cela aide quelqu'un d'autre :)

<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>
19
Distra

Cela créera une disposition avec une bordure supérieure de 2dp. il suffit de le définir comme arrière-plan de votre mise en page

<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:Android="http://schemas.Android.com/apk/res/Android">
    <item>
        <shape Android:shape="rectangle">
            <gradient
                Android:startColor="#4fc949"
                Android:centerColor="#0c87c5"
                Android:endColor="#b4ec51"
                Android:angle="180" />
        </shape>
    </item>
    <item Android:top="2dp">
        <shape Android:shape="rectangle">
            <solid Android:color="@color/background_color"/>
        </shape>
    </item>
</layer-list>
4
Alon Kogan

Cette source supplémentaire devrait résoudre votre problème

<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:Android="http://schemas.Android.com/apk/res/Android">
    <item>
        <shape Android:shape="rectangle">
            <gradient
                Android:angle="360"
                Android:centerColor="#e95a22"
                Android:endColor="#ff00b5"
                Android:gradientRadius="360"
                Android:startColor="#006386"
                Android:type="sweep" />
            <size Android:height="170dp"
                Android:width="170dp"/>
        </shape>
    </item>
    <item Android:top="2dp" Android:bottom="2dp" Android:right="2dp" Android:left="2dp">
        <shape Android:shape="rectangle">
            <size Android:width="140dp"
                Android:height="140dp"/>
            <solid Android:color="@color/colorAccent"/>
            <solid Android:color="@color/white"/>
        </shape>
    </item>
</layer-list>
1
Jay

Ce serait la solution appropriée de ce que vous voulez faire: cela inclut le dégradé dans le trait ainsi que le dégradé dans la couleur de remplissage

<?xml version="1.0" encoding="utf-8"?>
    <layer-list xmlns:Android="http://schemas.Android.com/apk/res/Android">
        <item>
            <shape >
                <gradient
                    Android:startColor="#2196F3"
                    Android:endColor="#673AB7"
                    Android:angle="270" />
                <stroke
                    Android:width="0dp"
                    Android:color="@color/transparentColor" />
                <corners
                    Android:radius="8dp" />
                <padding
                    Android:left="2dp"
                    Android:right="2dp"
                    Android:top="2dp"
                    Android:bottom="2dp" />
            </shape>
        </item>
        <item>
            <shape Android:shape="rectangle">

            </shape>
        </item>
        <item Android:top="0dp">
            <shape>
                <gradient
                    Android:startColor="#FBB100"
                    Android:endColor="#FF9900"
                    Android:angle="270"/>

                <corners
                    Android:radius="8dp" />
            </shape>
        </item>
    </layer-list>
0
Shivam Narula