在带有填充角的自定义按钮上添加单击效果

时间:2013-10-15 09:33:49

标签: android xml button

我正在尝试在xml创建的按钮上添加点击效果。到目前为止,我得到了clickeffect运行,但我无法提供点击效果与未点击的按钮上相同的圆角;这样看起来很可怕。在我的gradient.xml中,我提供了带圆角的相同图像布局,但它似乎忽略了此设置。

有人能指出我正确的方向吗?

enter image description here< ---> enter image description here


我的clickeffect.xml框:(box_with_click_effect.xml)

<selector xmlns:android="http://schemas.android.com/apk/res/android" >
<item android:state_focused="true" 
    android:state_pressed="false" 
    android:drawable="@drawable/rounded_box" />
<item android:state_focused="true" 
    android:state_pressed="true" 
    android:drawable="@drawable/gradient" />
<item android:state_focused="false" 
    android:state_pressed="true" 
    android:drawable="@drawable/gradient" />
<item android:drawable="@drawable/rounded_box" />
</selector>

我的button-drawable-xml:(rounded_box.xml)

<shape xmlns:android="http://schemas.android.com/apk/res/android" >    
   <solid android:color="@color/my_orange"/>
   <corners android:radius="15px"/>
   <padding android:left="1dp" android:top="1dp" android:right="1dp" android:bottom="1dp" /> 
</shape>

我的针对点击状态的gradient.xml:(gradient.xml)

<layer-list xmlns:android="http://schemas.android.com/apk/res/android" >
<item>
   <!--  THIS DOESN'T WORK: <bitmap android:src="@drawable/rounded_box"/> 
           SO I TRY THIS:   -->
   <shape xmlns:android="http://schemas.android.com/apk/res/android" >   
        <solid android:color="@color/cocus_orange"/>
        <corners android:radius="15px"/>
        <padding android:left="1dp" android:top="1dp" android:right="1dp" android:bottom="1dp" /> 
    </shape>
</item>
<item>
    <shape xmlns:android="http://schemas.android.com/apk/res/android">
        <gradient android:angle="90" 
            android:startColor="#880f0f10" 
            android:centerColor="#880d0d0f" 
            android:endColor="#885d5d5e"/>
    </shape>
</item>
</layer-list>

2 个答案:

答案 0 :(得分:1)

将您的代码更改为:

<layer-list xmlns:android="http://schemas.android.com/apk/res/android" >
<item>
   <!--  THIS DOESN'T WORK: <bitmap android:src="@drawable/rounded_box"/> 
           SO I TRY THIS:   -->
   <shape xmlns:android="http://schemas.android.com/apk/res/android" >   
        <solid android:color="@color/cocus_orange"/>
        <corners android:radius="15px"/>
        <padding android:left="1dp" android:top="1dp" android:right="1dp" android:bottom="1dp" /> 
    </shape>
</item>
<item>
    <shape xmlns:android="http://schemas.android.com/apk/res/android">
        <gradient android:angle="90" 
            android:startColor="#880f0f10" 
            android:centerColor="#880d0d0f" 
            android:endColor="#885d5d5e"/>

            <corners
                android:radius="15px" />
  </shape> 
</item>
</layer-list>

您可以使用此网站为您生成按钮:android button maker

答案 1 :(得分:0)

如果需要请尝试:

通过这个我们可以避免为状态按下和正常添加单个文件。但服务很完美。

   <?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android" >
 <item android:state_pressed="true" >
     <shape android:shape="rectangle"  >
         <corners android:radius="5dip" />
         <solid android:color="#a4c639"/>
         <stroke android:width="1px" android:color="#0dbcbf" />
     </shape>
 </item>
<item android:state_focused="true">
     <shape android:shape="rectangle"  >
         <corners android:radius="5dip" />
         <stroke android:width="1dip" android:color="#a4c639" />
         <solid android:color="#a4c639"/>       
     </shape>
 </item>  
<item >
    <shape android:shape="rectangle"  >
         <corners android:radius="5dip" />
<solid android:color="#0dbcbf"/>
</shape>
 </item>
</selector>