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

时间:2015-05-01 06:43:34

标签: 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_menu_button_background" />
</selector>

以及以下活动的xml:

<LinearLayout
    android:layout_width="0dp"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:background="#FF0000"
    android:weightSum="2">


    <Button
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:text="settings"
        android:id="@+id/btnMainMenuSettings"
        android:layout_gravity="center_horizontal"
        android:background="@drawable/main_menu_button_selector"
        android:layout_weight="1" />

    <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" />
</LinearLayout>

这是我的自定义视图:

<?xml version="1.0" encoding="utf-8"?>

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal"
    android:layout_gravity="center"
    android:gravity="center">

    <ImageView
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:src="@drawable/ic_launcher" />

    <TextView
        style="@style/main_menu_button"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:gravity="center"
        android:text="@string/app_name" />
</LinearLayout>

这是java代码:

public class CustomMainMenuButton extends LinearLayout implements View.OnClickListener {

    private Context mContext;

    public CustomMainMenuButton(Context context, AttributeSet attrs) {
        super(context, attrs);
        mContext = context;
        LayoutInflater inflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        inflater.inflate(R.layout.custom_main_menu_button, this, true);
    }

    @Override
    public void onClick(View v) {

    }
}

选择器适用于该按钮,但对于自定义视图,我只获得正常状态,自定义视图不可点击,当我点击自定义视图时没有任何反应。任何想法我怎么能让它工作?

3 个答案:

答案 0 :(得分:3)

您的布局无法点击,它是所有布局的默认状态。使用 自定义视图实现中的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" />

答案 1 :(得分:2)

尝试将选择器更改为:

<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>

答案 2 :(得分: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" />