我有一个相对布局
<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/