在我的活动中,我必须展示一些图片。我在我的活动中使用了gridview,每行应该显示4个图像(每个图像的宽度是屏幕的25%)。我需要用宽度来减小相同尺寸的图像高度。
所以,在我的UI中,我有一个gridview,我的imageAdapter将图片排列到屏幕上。这是适配器的代码:
public class ContestantsPhotoAdapter extends BaseAdapter {
private LayoutInflater myInflater;
private Bitmap[] bitmapList;
public ContestantsPhotoAdapter(Context c) {
myInflater = LayoutInflater.from(c);
}
public void setData(Bitmap[] bmImages){
bitmapList = bmImages;
Log.i("ContestantsPhotoAdapter", "Images passed to the adapter.");
}
@Override
public int getCount() {
return bitmapList.length;
}
@Override
public Object getItem(int position) {
return position;
}
@Override
public long getItemId(int position) {
return position;
}
public View getView(int position, View convertView, ViewGroup parent){
ViewHolder holder;
if (convertView == null) {
convertView = myInflater.inflate(R.layout.grid_contestantsphoto, null);
holder = new ViewHolder();
holder.ivIcon = (ImageView) convertView.findViewById(R.id.imvContestantsPhoto_icon);
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
holder.ivIcon.setImageBitmap(bitmapList[position]);
return convertView;
}
static class ViewHolder {
ImageView ivIcon;
}
}
grid_contestantsphoto.xml是:
<?xml version="1.0" encoding="utf-8"?>
<ImageView
xmlns:android = "http://schemas.android.com/apk/res/android"
android:id = "@+id/imvContestantsPhoto_icon"
android:layout_width = "wrap_content"
android:layout_height = "100dip"
android:contentDescription = "@string/menu_contentdescription" />
我的问题是,当我更改android:layout_height = "100dip"
时,它不会生效,并且始终高度与以前相同。
请告诉我如何减小尺寸。谢谢。
答案 0 :(得分:0)
这对我有用!请试试这个!
<ImageView
android:id="@+id/productImage"
android:layout_width="100px"
android:layout_height="100px"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:scaleType="fitCenter"
android:layout_marginRight="10dip"
android:adjustViewBounds="true"
android:src="@drawable/loading" />
答案 1 :(得分:0)
供将来参考。我将适配器代码更改为:
public void setData(Bitmap[] bmImages){
bitmapList = bmImages;
for(int i=0; i < bitmapList.length; i++){
bitmap = bitmapList[i];
bitmap = Bitmap.createScaledBitmap(bitmap, bitmap.getWidth(), bitmap.getWidth(), false);
bitmapList[i] = bitmap;
}
Log.i("ContestantsPhotoAdapter", "Images passed to the adapter.");
}
现在,图像的高度与宽度相同。