2011-04-28 63 views

回答

14

我遇到了這個,這就是我想出的。在您的佈局,將背景設置爲可繪製資源:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:id="@+id/clickable_layout" 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:background="@drawable/clickable"> 
... 
</LinearLayout> 

然後在繪製,添加clickable.xml像這樣:

<?xml version="1.0" encoding="utf-8"?> 
<selector xmlns:android="http://schemas.android.com/apk/res/android"> 
<item android:state_pressed="true" 
    android:drawable="@android:drawable/list_selector_background" />  
</selector> 

然後,它取決於你是否要添加在你的活動中點擊處理程序

0

您可以設置佈局中的所有元素clickable = false。然後,您應該通過將佈局背景設置爲某種顏色來模仿選擇行爲,並在單擊佈局時將所有其他背景設置爲透明背景。您可以使用佈局ID作爲索引來知道選擇了哪個佈局。

25

我更喜歡一個更簡單的方法:

<LinearLayout android:orientation="vertical" 
       android:id="@+id/layoutIdentifier" 
       android:clickable="true" 
       android:background="?android:attr/selectableItemBackground" 

       android:layout_width="match_parent" 
       android:layout_height="match_parent"> 

    <!-- put views here --> 
</LinearLayout> 

您不能更改狀態,按下背景這樣,但有時你並不真的需要。