我有以下簡單的選擇:的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