Android选择器在自定义视图中不工作

Android选择器在自定义视图中不工作,android,selector,android-custom-view,Android,Selector,Android Custom View,我有以下简单的选择器: <selector xmlns:android="http://schemas.android.com/apk/res/android"> <item android:drawable="@drawable/main_menu_button_background_pressed" android:state_pressed="true" /> <item android:drawable="@drawable/main_men

我有以下简单的选择器:

<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:drawable="@drawable/main_menu_button_background_pressed" android:state_pressed="true" />
    <item android:drawable="@drawable/main_menu_button_background" />
</selector>

对于按钮,选择器可以正常工作,但是对于自定义视图,我只获得正常状态,自定义视图不可单击,当我点击自定义视图时,不会发生任何事情。知道如何使其工作吗?

尝试将选择器更改为:

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

尝试将选择器更改为:

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

您的布局不可单击,它是所有布局的默认状态。使用
setClickable(true)
在自定义视图实现或xml布局中
android:clickable=“true”

你应该得到类似于:

<com.test.myapp.custom_views.CustomMainMenuButton
    android:layout_width="match_parent"
    android:layout_height="0dp"
    android:clickable="true"
    android:text="settings"
    android:layout_gravity="center_horizontal"
    android:background="@drawable/main_menu_button_selector"
    android:layout_weight="1" />

您的布局不可单击,它是所有布局的默认状态。使用
setClickable(true)
在自定义视图实现或xml布局中
android:clickable=“true”

你应该得到类似于:

<com.test.myapp.custom_views.CustomMainMenuButton
    android:layout_width="match_parent"
    android:layout_height="0dp"
    android:clickable="true"
    android:text="settings"
    android:layout_gravity="center_horizontal"
    android:background="@drawable/main_menu_button_selector"
    android:layout_weight="1" />

请在中添加
android:clickable=“true”

<com.test.myapp.custom_views.CustomMainMenuButton
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:text="settings"
        android:layout_gravity="center_horizontal"
        android:background="@drawable/main_menu_button_selector"
        android:layout_weight="1" />

请在中添加
android:clickable=“true”

<com.test.myapp.custom_views.CustomMainMenuButton
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:text="settings"
        android:layout_gravity="center_horizontal"
        android:background="@drawable/main_menu_button_selector"
        android:layout_weight="1" />


谢谢!!!我的选择器突然停止工作(可能是在我改变了一些东西但不知道是什么之后),我尝试了这里能找到的每一个解决方案,你的最终成功了。再次感谢谢谢你!!!我的选择器突然停止工作(可能是在我改变了一些东西但不知道是什么之后),我尝试了这里能找到的每一个解决方案,你的最终成功了。再次感谢