【问题标题】:Android: make list item with background clickableAndroid:使背景可点击的列表项
【发布时间】:2013-11-07 19:42:28
【问题描述】:

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

我的 ListView 项目的自定义布局:

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

我的自定义drawable 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>

现在这个项目不能再点击了。

你能解释一下为什么,以及我必须做些什么才能获得与单击按钮相同的行为(蓝色背景的选择器)吗?

【问题讨论】:

  • 我没有看到任何按下状态的选择器?
  • 我必须在drawable中定义它们?如何添加它以及在哪里添加?
  • 相信你会按照答案去做的;)

标签: android android-drawable android-selector


【解决方案1】:

res/drawable 文件夹中定义一个selector 及其presseddefault 状态(其中一个状态将是您的@drawable/item)。将其设置为列表行布局的背景。

查看类似问答:Selector on background color of TextView

编辑: 理解和应用谷歌所做的事情的最佳方法是研究 SDK 并做类似的事情。例如查看btn_default_holo_dark drawable。它是一个带有状态的选择器,是的,它是一个 xml。

这是一个取自 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) 拍摄的图像:

当您将此可绘制/选择器 (@drawable/btn_default_holo_dark) 应用于任何视图时,您将拥有它的状态。我希望这个示例能让我的答案更清楚。

【讨论】:

  • 但是我应该链接什么drawable,以便按下和选择状态看起来像按下按钮(带有蓝色覆盖)?我很欣赏代码 sn-ps...
  • 我已经编辑了我的答案。您可以在 sdk 使用 google 的图像。或者您可以通过图像编辑器创建它们。
  • 我现在明白了。我仍然觉得很烦人,我们必须复制 SDK 中的内容,而不是仅仅能够使用它并稍微调整一下......无论如何,非常感谢!
  • 这只是一个让事情变得清晰的示例。您可以直接使用带有 android.R.drawable. 标签的 android 资源,但您必须考虑它们不会存在于所有 api 级别中,并且可以针对 api 级别进行更改。如果您希望所有目标 API 具有相同的外观,您可以将它们复制到您的 res 文件夹并使用。或者您可以使用 Photoshop、gimp 等图形编辑器创建自己的图像。
  • 你确定吗?因为我不能引用 @android:drawable/btn_default_normal_holo_dark 或其中任何一个,而且我必须编写自己的选择器,而不是只使用默认的选择器来覆盖按下状态。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-05-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多