【问题标题】:XML selector background not working on 2.3XML 选择器背景不适用于 2.3
【发布时间】:2014-01-22 22:07:22
【问题描述】:

我有一个视图,我将选择器背景设置为应该触及的内容。确实如此,但仅在 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>

【问题讨论】:

  • 根据here的回答,可能是布局不可点击的问题。整个 API 可能存在差异。您是否尝试将其设置为可点击?
  • android:duplicateParentState="true" 成功了,非常感谢。

标签: android android-layout android-selector


【解决方案1】:

此功能自 API 1 以来已启用 - 所以我认为这与支持的操作系统级别无关,但更多与您的选择器代码格式不正确有关。

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

这是我使用的选择器之一(更完整,但仍然缺少一些关于焦点与按下的选项)。请记住,顺序很重要(这是从上到下评估的):

<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,从来没有遇到过问题..
  • 非常感谢,我一直在使用缩短的选择器版本,但我会记住这一点,以防万一。
猜你喜欢
  • 1970-01-01
  • 2011-02-07
  • 2017-02-13
  • 2020-09-15
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-05-19
相关资源
最近更新 更多