我想在按下时为图像视图的背景着色。我尝试使用选择器来更改不同状态(按下和未按下)的图像。现在的问题是图像用完全相同的大小替换了以前的图像,我希望它更大。我使用实现可检查的自定义布局,并且我还尝试重载 onCreateDrawableState 函数,但它从未被调用。
请帮忙..
这是它的外观:
按下前:
按下后:
这是项目列表自定义布局:
public class ListItem extends RelativeLayout implements Checkable, OnClickListener {
private View mItemChecked = null;
private ImageView imageSkinEdit = null;
/*private static final int[] CHECKED_STATE_SET = {
android.R.attr.state_pressed
};*/
public ListItem(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
}
public ListItem(Context context, AttributeSet attrs) {
super(context, attrs);
}
public ListItem(Context context) {
super(context);
}
@Override
protected void onFinishInflate() {
super.onFinishInflate();
try {
mItemChecked = this.findViewById(R.id.itemlist_checkedd);
imageSkinEdit = (ImageView)this.findViewById(R.id.skinEdit);
imageSkinEdit.setOnClickListener(this);
} catch (Exception e) {
}
}//onFinishInflate
@Override
public boolean isChecked() {
if(mItemChecked != null) {
return ((Checkable)mItemChecked).isChecked();
}
return false;
}
@Override
public void setChecked(boolean checked) {
if(mItemChecked != null) {
if (checked) {
mItemChecked.setVisibility(View.VISIBLE);
} else {
mItemChecked.setVisibility(View.GONE);
}
}
}
@Override
public void toggle() {
if(mItemChecked != null) {
((Checkable)mItemChecked).toggle();
}
}
@Override
public void onClick(View v) {
final int id = v.getId();
switch (id) {
case R.id.skinEdit:
//handleEdit();
break;
}//switch
}
@Override
public int[] onCreateDrawableState(int extraSpace) {
Log.d("onCreateDrawableState", "onCreateDrawableState");
return super.onCreateDrawableState(extraSpace + 1);
/*final int[] drawableState = super.onCreateDrawableState(extraSpace + 1);
if (isChecked()) {
Log.d("ListItem", "isChecked");
mergeDrawableStates(drawableState, CHECKED_STATE_SET);
}
return drawableState;*/
}
}
选择器:
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:state_pressed="true"
android:drawable="@drawable/ic_go_edit_pressed" />
<item
android:state_pressed="false"
android:drawable="@drawable/ic_go_edit" />
</selector>
图像视图 XML:
<ImageView
android:id="@+id/skinEdit"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_centerVertical="true"
android:adjustViewBounds="true"
android:scaleType="fitXY"
android:paddingLeft="10dip"
android:paddingRight="10dip"
android:src="@drawable/edit_skin_selector" />