我知道如何将onClick侦听器添加到LinearLayout以使整个布局成为单击目标,但我希望在点击时突出显示LinearLayout,就像列表视图中的列表项一样。做这件事最好的方法是什么?
发布于 2014-09-05 20:51:44
我更喜欢一种简单的方式:
<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>您不能以这种方式更改按下状态的背景,但有时您并不真正需要这样做。
发布于 2011-04-28 23:52:59
我遇到了这个,这就是我想出来的。在布局中,将背景设置为可绘制的资源:
<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>然后在drawable中,按如下方式添加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>然后,您可以决定是否要在活动中添加一个单击处理程序。
发布于 2012-06-06 21:43:39
您可以设置布局clickable=false中的所有元素。然后,您应该通过将布局背景设置为某种颜色来模拟选择行为,并在单击布局时将所有其他颜色设置为背景透明。您可以使用布局id作为索引,以了解选择了哪个布局。
https://stackoverflow.com/questions/5821044
复制相似问题