2014-11-20 56 views
12

我試圖在下列條件下使用的棒棒糖列表視圖控件:列表視圖設置自定義的紋波選擇

  1. 的主題類型是默認Theme.Material(黑暗的主題)。
  2. 列表視圖包含在具有白色背景的較大布局中。
  3. 列表視圖應該有一個以白色背景顯示的列表選擇器。

注:我不得不因爲如果我使用白色背景,主題選擇深色材料採用主題的colorControlHighlight顏色的波紋,這是40ffffff使用自定義的列表選擇顏色,並沒有顯示出來。

我第一次嘗試以下操作:

佈局XML

<LinearLayout 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:orientation="vertical" 
    android:background="@android:color/white" > 

    <TextView 
     android:id="@+id/title" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" /> 

    <ListView 
     android:id="@+id/list_view" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:listSelector="@drawable/list_selector" /> 

</LinearLayout> 

list_selector XML

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

</ripple> 

和列表視圖行XML

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:tools="http://schemas.android.com/tools" 
    android:layout_width="match_parent" 
    android:gravity="center_vertical" 
    android:orientation="horizontal" 
    tools:ignore="UseCompoundDrawables" > 

    <ImageView 
     android:id="@+id/list_image" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" /> 

    <TextView 
     android:id="@+id/list_title" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" /> 

</LinearLayout> 

我所期待看到那時候我選擇一個項目,該項目被選爲帶有#ff0000顏色的波紋。然而,這就是我最終看到:

enter image description here

我所希望的是有點接近這種行爲 - 但選擇列表行內限制!我究竟做錯了什麼?

感謝, 扎克

回答

26

您使用的是無界的脈動,例如沒有內容或遮罩層的波紋,所以波紋投影到其父級ListView的背景上。您應該設置遮罩層來約束波紋邊界。

RES /繪製/ my_list_selector.xml:

<?xml version="1.0" encoding="utf-8"?> 
<ripple xmlns:android="http://schemas.android.com/apk/res/android" 
    android:color="?attr/colorControlHighlight"> 
    <item android:id="@android:id/mask"> 
     <color android:color="@color/white" /> 
    </item> 
</ripple> 
+3

感謝Alanv!今天的另一個很好的迴應(你之前幫助過我的另一個漣漪問題!)。不勝感激。 – Zach 2014-11-20 21:05:44

+0

什麼是@ id/mask?我需要在其他地方定義它嗎? – poitroae 2015-08-11 07:32:19

+0

這應該是'@android:id/mask'。修復了代碼示例。 – alanv 2015-08-11 21:25:41

相關問題