如何在Android xml drawables中绘制带圆角的线条

时间:2016-06-11 11:01:39

标签: android xml drawable

我想在带有圆角的XML中绘制一条线。我没有得到如何创建它。我想将此行用于Seekbar。我想像矩形那样为线条提供圆角。

<?xml version="1.0" encoding="utf-8"?><layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item android:id="@android:id/background">
    <shape android:shape="line">
        <stroke
            android:width="5dp"
            android:color="#7b7878" />
    </shape>
</item>
<item android:id="@android:id/progress">
    <clip>
        <shape android:shape="line">
            <stroke
                android:width="5dp"
                android:color="#FF0000" />
        </shape>
    </clip>
</item>

2 个答案:

答案 0 :(得分:1)

您需要将StrokeCap()设置为Paint.Cap.ROUND

答案 1 :(得分:0)

你可以这样做

    //seek_bar.xml
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android" >
<item
    android:id="@android:id/background"
    android:drawable="@drawable/seek_bar_background"/>
<item android:id="@android:id/progress"
    android:drawable="@drawable/seek_bar_progress" />
</layer-list>

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

<gradient
    android:angle="270"
    android:startColor="#8a8c8f"
    android:endColor="#58595b" />

<corners android:radius="5dip" />

</shape>

//seek_bar_progress.xml
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android" >
<item
    android:id="@android:id/background"
    android:drawable="@drawable/seek_bar_background"/>
<item android:id="@android:id/progress">
    <clip android:drawable="@drawable/seek_bar_progress_fill" />
</item>

</layer-list>

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

<gradient
    android:startColor="#b3d27e"       
    android:endColor="#93c044"
    android:angle="270"
 />
 <corners android:radius="5dip" />  

</shape>

Ref link