android - 为什么我的RelativeLayout不使用选择器?

标签 android android-selector

我有一个相对布局

<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/container"
    android:layout_centerInParent="true"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:clickable="true"
    android:background="@drawable/animated_bg">

我定义了一个选择器

<ripple xmlns:android="http://schemas.android.com/apk/res/android"
android:color="@color/ripple_color">
<item>
    <selector>
        <item android:state_pressed="true"
            android:drawable="@color/grey_1" />

        <item android:state_focused="true"
            android:drawable="@color/grey_1" />

        <item android:drawable="@color/white" />
    </selector>
</item>

但是当我按下布局时它不会改变颜色。

有人能注意到为什么会发生这种情况吗?

更新

这是完整的 xml 文件

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/container"
    android:layout_centerInParent="true"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:clickable="true"
    android:background="@drawable/animated_bg">

    <RelativeLayout>

        <RelativeLayout>

            <ImageView />

            <LinearLayout>

                <TextView />

                <TextView />

            </LinearLayout>

        </RelativeLayout>

        <LinearLayout >

            <LinearLayout >

                <RelativeLayout >

                    <ImageView />

                    <TextView />

                    <TextView />

                    <TextView />

                </RelativeLayout>

                <RelativeLayout >

                    <ImageView />

                    <TextView />

                    <TextView />

                </RelativeLayout>

            </LinearLayout>

            <LinearLayout >

                <RelativeLayout >

                    <TextView />

                </RelativeLayout>
            </LinearLayout>
        </LinearLayout>

    </RelativeLayout>

</RelativeLayout>

最佳答案

我已经用它的选择器测试了你的布局。效果很好。但是,我不知道您的相对布局中是否还有其他 View /布局?如果 RelativeLayout 有 subview 。这些 View 可能会消耗您的事件press,这将导致背景不会改变其颜色。在我的测试中,我的相对布局内没有任何 subview 。你能发布你的整个布局文件吗?

更新: 如果您的 subview 不需要处理点击事件,您可以设置android:clickable="false",以便点击事件将传递给您的父级相对布局。如果不是这样的话,那么这个link可能对你有帮助。

关于android - 为什么我的RelativeLayout不使用选择器?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34954115/

相关文章:

android - 获取 Paypal 支付状态为 "DEAD"

java - 如何在 android 中以编程方式将 TextViews 数组添加到相对布局中而不重叠?

iphone - 在按下状态和选择状态时更改 UITableViewCell 的颜色

Android:如何在一个 Vector Drawable xml 中使用选择器更改 android:fillcolor

android - 导航 View 的选择器行为不正确

java - 解析 JSON 文件

android - 用于测试 Android 客户端应用程序的自定义 Web 服务

java - fragment 访问其他java类

带有圆角的 Android SlidingTabs 样式选项卡

Android:如何制作一个可绘制的选择器