Android 选择器在自定义视图中不起作用
Android selector not working in 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>
和以下 activity 的 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) {
}
}
选择器适用于按钮,但对于自定义视图,我只得到正常状态,自定义视图不可点击,当我点击自定义视图时没有任何反应。知道如何让它发挥作用吗?
尝试将选择器更改为:
<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" />
请在
中添加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" />
我有以下简单的选择器:
<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>
和以下 activity 的 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) {
}
}
选择器适用于按钮,但对于自定义视图,我只得到正常状态,自定义视图不可点击,当我点击自定义视图时没有任何反应。知道如何让它发挥作用吗?
尝试将选择器更改为:
<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" />
请在
中添加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" />