XML选择器背景在2.3上无法正常工作

5
我有一个视图,我设置了一个选择器背景,应该可以触摸到。在4.x上可以,但在2.3上无法响应触摸。可能的问题是什么?以下是布局代码:
<ImageView
    android:id="@+id/imageView1"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:adjustViewBounds="true"
    android:src="@drawable/idee_baden"
    android:scaleType="centerInside" />

<RelativeLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_alignParentTop="true"
    android:background="@drawable/background_selector" >

    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_centerVertical="true"
        android:layout_margin="8dp"
        android:ellipsize="end"
        android:padding="4dp"
        android:singleLine="true"
        android:textAppearance="@style/SmallTextBold"
        android:textColor="#ffffff" />
</RelativeLayout>

这是background_selector.xml文件的内容:

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:drawable="@drawable/blue9"
          android:state_pressed="true" />
    <item android:drawable="@drawable/black9" />
</selector>

3
根据这里的回答(https://dev59.com/o2ct5IYBdhLWcg3wc9Eg#12081273),问题可能是布局不可点击。在API中可能存在差异。您尝试过将其设置为可点击吗? - MalaKa
5
android:duplicateParentState="true" 起了作用,非常感谢。 - fweigl
1个回答

1

此功能自API 1启用,因此我认为这与操作系统级别的支持无关,而更多地与您的选择器代码格式不佳有关。

我认为您的选择器格式不正确。 我认为您的“item”标记部分应该填写得更完整,并且应该具有更多的标记。

以下是我使用的其中一个选择器(更完整,但仍缺少有关焦点与按下选项的某些选项)。 请记住,顺序很重要(从上到下进行评估):

<selector
    xmlns:android="http://schemas.android.com/apk/res/android">
    <item
        android:drawable="@drawable/circle_button_on"
        android:state_focused="true"
        android:state_pressed="true"/>
    <item
        android:drawable="@drawable/circle_button_on"
        android:state_focused="false"
        android:state_pressed="true"/>
    <item
        android:drawable="@drawable/circle_button_off"
        android:state_focused="true"
        android:state_pressed="false"/>
    <item
        android:drawable="@drawable/circle_button_off"
        android:state_focused="false"
        android:state_pressed="false"/>
</selector>

你确定这会有所不同吗?对我来说似乎很奇怪,因为我一直认为只要在选择器中包含android:state_pressed,其他状态并不重要。我曾经使用过只包含android:state_pressed的选择器,从未遇到过问题。 - MalaKa
非常感谢,我一直在各个地方使用缩短的选择器版本,但以防万一,我会记住这个。 - fweigl

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接