进行线性布局可选择就像在一个列表视图列表项(安卓) [英] Make linear layout selectable like a list item in a list view (Android)

查看:126
本文介绍了进行线性布局可选择就像在一个列表视图列表项(安卓)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道如何一个onClick监听器添加到的LinearLayout,使整个布局点击目标,但我想有LinearLayout中得到突出显示时就像在列表视图列表项挖掘。什么是做到这一点的最好方法是什么?

I know how to add an onClick listener to a LinearLayout to make the whole layout a click target, but I'd like to have the LinearLayout get highlighted when tapped just like a list item in a list view. What's the best way to do this?

推荐答案

我遇到了这一点,这是我想出了。在您的布局,将背景设置为一个绘制资源:

I ran into this and this is what I came up with. In your layout, set the background to a drawable resource:

<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像这样:

Then in drawable, add clickable.xml as so:

<?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>

然后就看你,你是否要添加一个单击处理程序在你的活动。

Then it's up to you whether or not you want to add a click handler in your activity.

这篇关于进行线性布局可选择就像在一个列表视图列表项(安卓)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆