2013-10-15 33 views
1

我正在嘗試在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> 

我的按鈕抽拉的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> 
+0

爲什麼不使用九個補丁繪製背景http://developer.android.com/guide/topics/graphics/2d-graphics.html#nine-patch。這裏有一個博客http://www.dibbus.com/2011/03/9patch-images-in-android/ – Raghunandan

+0

,因爲我是一名新手,我想習慣通過xmls進行造型。 – bofredo

回答

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

+0

thx爲鏈接,爲它添加書籤。現在我會試試你的方法 – bofredo

+0

我把放在 -block內,它工作正常。我完全沒有想到在gradient.xml中的第二個是轉換後的樣子。謝謝你,威爾瑪...... errr dyna! – bofredo

+1

ahah歡迎你的朋友! :d – Dyna

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> 
相關問題