我有以下简单的选择:的Android选择自定义视图中不工作
<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) {
}
}
选择器对按钮正常工作,但对于自定义视图,我只获得正常状态,自定义视图不可点击,并且在点按自定义视图时没有任何反应。任何想法我怎么能使它工作?
谢谢大家。这一个伎俩。 – dsb