Android: Stroke Left Side of Button Only

家住魔仙堡 提交于 2019-12-12 01:43:02

问题


I'm trying to draw highlight "3D" lines on a button using a stroke. How do I do this? The below chunk from my custom_button.xml selector is for the default button state. I'm expecting the background of the button to be filled with a blue gradient but a pink (just for testing!) line to appear on the left side of the button.

Should I be using a layer-list as shown below? How do I get the line to appear on just the left of the button? At present the pink line appears around the entire button and it fills the button with black instead of the gradient blue. Is this because the second item's default color is black.. even though I am trying to set it to transparent?

<shape>
    <gradient
        android:endColor="@color/blue2"
        android:startColor="@color/blue25"
        android:angle="270" />
    <stroke
        android:width="3dp"
        android:color="#00FFFF" />
    <corners
        android:radius="3dp" />
    <padding
        android:left="10dp"
        android:top="10dp"
        android:right="10dp"
        android:bottom="10dp" />
</shape>

<item android:left="1dp">
    <shape>
       <solid android:color="@android:color/transparent" /> 
       <stroke android:width="1dp" android:color="#FF8888" />
   </shape>
</item>

</layer-list>

Below is an image of what I get (on top) and what I expect to get (on the bottom). I'm only using these colours for test purposes! :-)


回答1:


You can try this

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

    <item
        android:bottom="-2dp"
        android:right="-2dp"
        android:top="-2dp">
        <shape>
            <solid android:color="@android:color/transparent" />

            <stroke
                android:width="1dp"
                android:color="#FF8888" />
        </shape>
    </item>
    <item
        android:drawable="@drawable/shape2"
        android:left="1dp">
    </item>

</layer-list>

Where @drawable/shape2 is the shape you specified above

result:



来源:https://stackoverflow.com/questions/24153167/android-stroke-left-side-of-button-only

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!