2016-03-03 229 views
0

所以我有一個RecyclerView,它是由我的自定義佈局(代碼如下)填充。我製作了一個可繪製的背景,並將其設置在我的自定義佈局的根部,以便用戶單擊該項目時,背景顏色會發生變化。每個項目的佈局上還有一個選項按鈕(3個點),這也是可點擊的,並且在點擊時還需要更改背景顏色。Android Relativelayout點擊背景行爲

的問題是,當我把我作爲繪製背景的選項菜單點擊(下面的屏幕)

當我設置選項菜單上的背景"?android:attr/selectableItemBackground",一切正常,當整個佈局不變色精細。

代碼爲我的佈局:

<RelativeLayout xmlns:card_view="http://schemas.android.com/apk/res-auto" 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="fill_parent" 
    android:layout_height="60dp" 
    android:background="@drawable/item_click" 
    android:layout_marginBottom="2dp"> 
    <ImageView 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_centerVertical="true" 
     android:paddingLeft="16dp" 
     android:id="@+id/item_icon" 
     android:src="@drawable/ic_folder" /> 
    <LinearLayout 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:orientation="vertical" 
     android:paddingLeft="72dp" 
     android:layout_centerVertical="true"> 
     <TextView 
      android:id="@+id/item_name" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:textColor="#333333" 
      android:text="Caption" 
      android:textSize="13sp" /> 
     <TextView 
      android:id="@+id/item_description" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:textColor="#333333" 
      android:text="Description" 
      android:textSize="13sp" /> 
    </LinearLayout> 
    <ImageView 
     android:layout_width="wrap_content" 
     android:layout_height="match_parent" 
     android:layout_alignParentRight="true" 
     android:foregroundGravity="center_vertical" 
     android:paddingLeft="16dp" 
     android:paddingRight="16dp" 
     android:id="@+id/item_menu" 
     android:src="@drawable/ic_ellipsis_v" 
     android:background="@drawable/item_click" /> 
</RelativeLayout> 

代碼提拉:

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

項目時的選項菜單的背景設置爲"?android:attr/selectableItemBackground"選擇(我想整行變色到藍色): Screenshot1

當選項菜單的背景設置爲我的可繪製時選擇的項目: Screenshot2

回答

1

更改您選擇的默認狀態:

<selector xmlns:android="http://schemas.android.com/apk/res/android"> 
<item 
    android:state_focused="true" 
    android:state_pressed="true" 
    android:drawable="@color/primary_100" /> 
<item 
    android:state_focused="false" 
    android:state_pressed="true" 
    android:drawable="@color/primary_100" /> 
<item 
    android:drawable="@android:color/transparent" /> 
</selector> 
+0

但我想改變點擊背景顏色。 selectableItemBackground具有默認顏色 –

+0

您不能兩面都有。你的圖像背景是白色的,所以它會覆蓋其容器的背景。創建一個選擇器,其中默認狀態是透明的,而不是白色。看到我編輯的答案。 – Francesc

+0

你的意思是像圖像文件(PNG)背景是白色的? –