我对GridView中单元格的高度有问题.我的每个单元格都会有一个imageView,如下所示:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<GridView
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:verticalSpacing="10dp"
android:horizontalSpacing="10dp"
android:id="@+id/eventGridView" android:numColumns="2"/>
</LinearLayout>
Run Code Online (Sandbox Code Playgroud)
这是每个单元格的ImageView
<com.example.scheduling_android.view.RoundedCornerImageView
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:maxWidth="150dp"
android:maxHeight="150dp"
xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/eventImageView"
android:adjustViewBounds="false"/>
Run Code Online (Sandbox Code Playgroud)
我正在为我的imageView玩不同的图像大小.我得到的是,每当我的图像变大(例如,400 x 400px)时,单元格的高度变得非常高(大约是宽度的两倍,请注意我将网格设置为使用2列).但是,当我将图像保存为较小的尺寸(例如,160 x 160px)时,gridView正确显示.高度和宽度匹配.
这是一个截图:

什么似乎是问题?可能是大图像导致gridView错误地计算其单元格的高度?我该怎么做才能解决这个问题?
我有以下代码用于渲染带圆角的imageView.
public class RoundedCornerImageView extends ImageView {
private int rounded;
public RoundedCornerImageView(Context context) {
super(context);
}
public RoundedCornerImageView(Context context, AttributeSet attrs) {
super(context, attrs);
}
public RoundedCornerImageView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
}
public int getRounded() {
return rounded;
}
public void setRounded(int rounded) {
this.rounded = rounded;
}
@Override
public void onDraw(Canvas canvas)
{
Drawable drawable = getDrawable();
int w = drawable.getIntrinsicHeight(),
h = drawable.getIntrinsicWidth();
Bitmap rounder = Bitmap.createBitmap(w,h,Bitmap.Config.ARGB_8888);
Canvas tmpCanvas = new Canvas(rounder);
// …Run Code Online (Sandbox Code Playgroud)