2015-12-07 132 views
3

您可以在CyanogenMod的音樂應用看看這個列表項:Android中的材質圓形按鈕?

enter image description here

哪個 View正在使用該設置按鈕

?它不可能是一個ImageButton因爲當我按下它,它會創建一個圓形的波紋:

enter image description here

也許一個做到這一點的方法是在Toolbar充氣列表內的菜單按鈕。但是調用inflateMenu()需要最低的API級別21.可以使用其他方式來實現此目的?謝謝!

+0

是的,但這不是'ActionBar'或'Toolbar',它是一個列表項。 –

+0

所有你需要它來定義一個可繪製的圓形波紋 - 甚至是一個無限的波紋。無界的波紋默認爲圓形。你問你如何定義波紋的形狀? –

+0

請演示xml作爲答案,一旦我回家,我會嘗試一下。謝謝! –

回答

5

如果你想有一個圓形波紋,你可以用兩種方法來定義它。

  1. 您可以使用無限漣漪 - 如果沒有定義形狀或掩蓋紋波默認爲圓形
  2. 可以具體界定的屏蔽,圓形的形狀

,無界的脈動,定義非常簡單地像這樣:

<ripple 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    android:color="@android:color/darker_gray" /> 

如果你碰個e View它會創建基本的循環波紋,例如,當您輸入PIN碼時,會在默認Android鎖定屏幕上使用該波紋。


然而無限的漣漪根據定義沒有任何界定的邊界,因此並不總是你想要的。如果您只是想特別定義波紋的形狀和大小,則可以使用蒙版。這樣做的好處是用於定義形狀的項目不會被繪製。它的工作原理是這樣的:

<ripple 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    android:color="@android:color/darker_gray"> 

    <!-- If you give an item this mask id then it will only be used to determine the shape of --> 
    <!-- ripple, but will not be drawn in any way --> 
    <item android:id="@android:id/mask"> 
     <!-- android:shape defines the shape of the ripple. I use oval for a circular one, --> 
     <!-- but there are also rectangle, line, ring --> 
     <shape android:shape="oval"> 
      <solid android:color="@android:color/white"/> 
     </shape> 
    </item> 
</ripple> 

你可以從這些兩個選項以上選擇 - 這曾經你最喜歡的。只是紋波分配給ImageView這樣,它應該看起來非常像你的問題的圖像:

<ImageView 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:background="@drawable/ripple" 
    android:src="@drawable/your_icon" 
    android:clickable="true" /> 

我加入了clickable屬性之上,因此紋波將被啓用,即使你不指定點擊傾聽ImageView。只要你分配一個點擊監聽器,你並不需要它。

+0

雖然這肯定是做正確的方式,它可能是值得一提,它需要最低API等級21 –

+0

@HendraAnggrian當然。 RippleDrawable被添加到API級別21中。最簡單的回退是一個簡單的選擇器,可用於以前的API級別。 –