我有这个 StateListDrawable:
GradientDrawable gd = new GradientDrawable(Orientation.TOP_BOTTOM, new int[] {
firstColor, secondColor });
StateListDrawable sld = new StateListDrawable();
sld.addState(new int[] { android.R.attr.state_pressed },
new ColorDrawable(onClickColor));
sld.addState(StateSet.WILD_CARD, gd);
这是我的布局:
<?xml version="1.0" encoding="utf-8"?>
<merge xmlns:android="http://schemas.android.com/apk/res/android" >
<RelativeLayout
android:id="@+id/actionBar"
android:layout_width="fill_parent"
android:layout_height="48dp"
android:baselineAligned="true"
android:descendantFocusability="afterDescendants">
<ImageButton
android:id="@+id/actionButtonBack"
android:layout_width="48dip"
android:layout_height="48dip"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true" />
<ImageButton
android:id="@+id/actionButton3"
android:layout_width="48dip"
android:layout_height="48dip"
android:layout_alignParentTop="true"
android:layout_toLeftOf="@+id/actionButton2" />
<ImageButton
android:id="@+id/actionButton2"
android:layout_width="48dip"
android:layout_height="48dip"
android:layout_alignParentTop="true"
android:layout_alignWithParentIfMissing="true"
android:layout_toLeftOf="@+id/actionButton1" />
<ProgressBar
android:id="@+id/actionButton1"
android:layout_width="48dip"
android:layout_height="48dip"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:layout_alignWithParentIfMissing="true"
android:padding="4dip" />
<TextView
android:id="@+id/tvTitle"
android:layout_width="wrap_content"
android:layout_height="48dip"
android:layout_alignParentTop="true"
android:layout_alignWithParentIfMissing="true"
android:layout_toLeftOf="@+id/actionButton3"
android:layout_toRightOf="@+id/actionButtonBack"
android:gravity="center"
android:textSize="14dip"
android:textStyle="bold" />
</RelativeLayout>
</merge>
然后我像这样设置drawable:
tv.setClickable(false);
tv.setTextColor(textViewColor);
actionBack.setBackgroundDrawable(sld);
action2.setBackgroundDrawable(sld);
action3.setBackgroundDrawable(sld);
tv.setBackgroundDrawable(sld);
pb.setBackgroundDrawable(sld);
当我单击 actionBack 按钮时,整个布局变成我拥有的颜色,如 onClickButton。
我只是希望它表现得好像我按下了一个常规 xml 定义的可绘制按钮。
我究竟做错了什么?