如何在gridview中更改imageView的高亮颜色。
我试过了,
public View getView(int position, View convertView, ViewGroup parent) {
ImageView imageView;
if (convertView == null) { // if it's not recycled, initialize some attributes
imageView = new ImageView(mContext);
imageView.setLayoutParams(new GridView.LayoutParams(width, height));
imageView.setScaleType(ImageView.ScaleType.FIT_XY);
imageView.setBackgroundResource(R.drawable.menu_beh);
// imageView.setPadding(8, 8, 8, 8);
} else {
imageView = (ImageView) convertView;
}
String s=(String)HiveApp.mgd[position].posters[2].image.url;
// imageView.setImageDrawable(getPicture(items[position]));
HiveApp.id.download(s, imageView);
// id.DisplayImage(s, imageView);
return imageView;
}
答案 0 :(得分:13)
我自己解决,你应该将它添加到你的布局xml
android:listSelector="@drawable/panel_picture_frame_background"
而不是这个
imageView.setBackgroundResource(R.color.gridview_highlight_selector);
感谢
答案 1 :(得分:3)
将包含以下内容的imageview_highlight_selector.xml
文件添加到drawable
文件夹,然后调用imageView.setBackgroundResource(R.drawable.gridview_highlight_selector);
。
<?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/highlight_bg" /> <!-- pressed -->
<item android:drawable="@drawable/normal_bg" /> <!-- default -->
</selector>
我建议您在xml文件中定义gridview项,然后从Java代码中扩充该xml,这将更整洁。
修改强>
如果您只想使用颜色而不是drawable,可以将color
子文件夹添加到res
文件夹,并将以下内容gridview_highlight_selector.xml
添加到color
}文件夹,并在您的代码中调用imageView.setBackgroundResource(R.color.gridview_highlight_selector);
:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed="true" android:color="#ffff" />
<item android:color="#ff3697de" />
</selector>