Multi color Stroke image with XML

2019-09-03 23:39发布

问题:

Does it possible to create multi-color stroke with the help of XML? I want to create exact view attached here.

Thanks

回答1:

You could use a trick by creating a drawable shape with gradient and make it a parent's background... something like this:

gradient_stroke.xml (in res/drawable):

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item>
    <shape  android:shape="rectangle" >
        <gradient
                     android:angle="180"
                     android:startColor="#fff96200"
                     android:centerColor="#fffff60f"
                     android:endColor="#fff96200"
                     android:centerX="50%"

                     >
             </gradient>
        <corners android:radius="5dp"></corners>
    </shape>
</item>

your_view.xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
          android:orientation="vertical"
          android:layout_width="wrap_content"
          android:padding="2dp"
          android:background="@drawable/gradient_stroke"
          android:layout_height="wrap_content">

<Button android:text="Submit"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:background="@color/green"
        android:id="@+id/button"/>
</LinearLayout>

Just an idea - play around with it to get desired values...