【问题标题】:Adding a click-effect on a custom button with padded corners在带有填充角的自定义按钮上添加点击效果
【发布时间】:2013-10-15 09:33:48
【问题描述】:

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

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


我的盒子带有 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>

【问题讨论】:

标签: android xml button


【解决方案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

【讨论】:

  • 感谢链接,将其添加为书签。现在我会试试你的方法
  • 我将 放在 块内,它工作正常。我完全没有想到 gradient.xml 中的第二个 是过渡后的样子。谢谢你,威尔玛... errr dyna!
【解决方案2】:

如果需要请尝试:

这样我们可以避免为按下状态和正常状态添加单独的文件。但服务完美。

   <?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>

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-09-02
    • 2016-07-24
    • 2015-09-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多