Android:使具有背景可点击背景的列表项 [英] Android: make list item with background clickable

查看:118
本文介绍了Android:使具有背景可点击背景的列表项的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个列表,其中已使用自定义布局定义了自己的列表视图项.此布局的背景带有自定义可绘制对象.

I have a list where I have defined my own list view items with a custom layout. This layout has a background with a custom drawable.

我对ListView项的自定义布局:

My custom layout for the ListView item:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@drawable/item"
    android:clickable="true"
    android:focusable="true"
    android:focusableInTouchMode="true" >
    ...
</RelativeLayout>

我的自定义可绘制 item.xml :

<layer-list xmlns:android="http://schemas.android.com/apk/res/android" >

    <!-- background: shadow -->
    <item>
        <shape
            android:dither="true"
            android:shape="rectangle" >
            <corners android:radius="2dp" />

            <solid android:color="@color/itemShadowColor" />
        </shape>
    </item>

    <!-- foreground: surface -->
    <item android:bottom="2dp">
        <shape
            android:dither="true"
            android:shape="rectangle" >
            <corners android:radius="2dp" />

            <solid android:color="@color/itemBackgroundColor" />
        </shape>
    </item>

</layer-list>

现在该项目不再可用.

您能解释一下为什么吗,以及我要怎么做才能像单击按钮一样具有相同的行为(蓝色背景的选择器)?

Can you explain me why, and what I have to do to have the same behavior (selector with the blue background) like a button click?

推荐答案

使用 pressed default 状态定义选择器> res/drawable 文件夹(状态之一就是您的 @ drawable/item ).将其设置为列表行布局的背景.

Define a selector with its pressed and default states in res/drawable folder(one of the state will be your @drawable/item). Set it as the bg of your list row layout.

查看类似的问答:选择TextView的背景颜色

修改:理解和应用类似Google的最佳方法是调查SDK并执行类似的操作.例如,查看 btn_default_holo_dark 可绘制对象.这是一个带有状态的选择器,是的,它是一个xml.

Best way to understand and apply something like google did is, to look into SDK and do similar things to that. For instance look at the btn_default_holo_dark drawable. It is a selector with states and yes it is a xml.

这是从sdk( sdk \ platforms \ android-18 \ data \ res \ drawable \ btn_default_holo_dark.xml )提取的选择器

This is a selector taken from sdk (sdk\platforms\android-18\data\res\drawable\btn_default_holo_dark.xml)

<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:state_window_focused="false" android:state_enabled="true"
        android:drawable="@drawable/btn_default_normal_holo_dark" />
    <item android:state_window_focused="false" android:state_enabled="false"
        android:drawable="@drawable/btn_default_disabled_holo_dark" />
    <item android:state_pressed="true" 
        android:drawable="@drawable/btn_default_pressed_holo_dark" />
    <item android:state_focused="true" android:state_enabled="true"
        android:drawable="@drawable/btn_default_focused_holo_dark" />
    <item android:state_enabled="true"
        android:drawable="@drawable/btn_default_normal_holo_dark" />
    <item android:state_focused="true"
        android:drawable="@drawable/btn_default_disabled_focused_holo_dark" />
    <item
         android:drawable="@drawable/btn_default_disabled_holo_dark" />
</selector>

这些是从sdk( sdk \ platforms \ android-18 \ data \ res \ drawable-xhdpi )拍摄的图像:

These are the images taken from sdk (sdk\platforms\android-18\data\res\drawable-xhdpi):

当将此可绘制/选择器( @ drawable/btn_default_holo_dark )应用于任何视图时,将具有其状态.我希望这个样本可以使我的答案更加清楚.

When you apply this drawable/selector (@drawable/btn_default_holo_dark) to any view, you are going to have its states. I hope this sample makes my answer more clear.

这篇关于Android:使具有背景可点击背景的列表项的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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