2011-04-28 143 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> 

您不能更改状态,按下背景这样,但有时你并不真的需要。