使用选择器作为视图的背景,如下面的代码:
my_selector.xml:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android" >
<item android:state_pressed="true" android:drawable="@drawable/cell_bg_e" />
<item android:state_checked="true" android:drawable="@drawable/cell_bg_e" />
<item android:state_selected="true" android:drawable="@drawable/cell_bg_e" />
<item android:drawable="@drawable/cell_bg_n_trans" />
</selector>
MyView.java
public class MyView extends LinearLayout
{
public MyView(Context context, CharSequence text, Drawable drawable) {
super(context);
setBackgroundResource(R.drawable.my_selector);
}
}
它适用于所有设备,除了某些特定的800x480分辨率设备(lick htc g12)
为什么?
答案 0 :(得分:0)
您应确保将此选择器可绘制的视图设置为可点击的背景。
您可以在代码中执行此操作:
public MyView(Context context, CharSequence text, Drawable drawable) {
super(context);
setClickable(true);
setBackgroundResource(R.drawable.my_selector);
}
或者用XML:
<MyView
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@drawable/my_selector"
android:clickable="true"
android:orientation="horizontal"/>