Android 使GridView项完全垂直

Android 使GridView项完全垂直,android,android-layout,gridview,android-listview,Android,Android Layout,Gridview,Android Listview,我使用栅格视图显示我的数据[即图像及其倾斜] 我的图像是用imageloader库动态加载的。问题是我的一些图片是风景画或肖像画。由于这个原因,我的网格元素不能保持精确的平方比。有些垂直拉伸或压缩 <GridView android:id="@+id/fcpl_grid_listview" android:layout_width="match_parent" android:layout_height="match_parent" android:numC

我使用栅格视图显示我的数据[即图像及其倾斜]

我的图像是用imageloader库动态加载的。问题是我的一些图片是风景画或肖像画。由于这个原因,我的网格元素不能保持精确的平方比。有些垂直拉伸或压缩

<GridView
    android:id="@+id/fcpl_grid_listview"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:numColumns="2"
    android:scrollingCache="false"/>

我在网上尝试了不同的建议方法,但都不起作用,因为我有动态图像加载,并且调用getView时不知道imageView布局参数。

我建议您创建一个类,扩展
imageView
(或
GridView
)并测量根视图或任何其他父视图的边界。然后在项目xml中使用它

public class SquareImageView extends ImageView {
    public SquareImageView(Context context) {
        super(context);
    }

    public SquareImageView(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public SquareImageView(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
    }

    @Override
    public void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        int width = MeasureSpec.getSize(widthMeasureSpec);
        int height = MeasureSpec.getSize(heightMeasureSpec);
        View rootView = this.getRootView();
        if (rootView != null) {
            width = rootView.getWidth();
            height = rootView.getHeight();
        }
        int size = width > height ? height : width;
        super.onMeasure(size, size);
        setMeasuredDimension(size, size);
    }
}

你为什么不确定你的
图像视图的高度和宽度呢;我必须保持GridView宽度作为填充父对象;网格视图中有两列。所以我不知道确切的高度[即高度=宽度],你只需要固定图像的高度。高度应该与宽度完全相同[并且宽度是动态的,对于不同的屏幕会有所不同]@AabidMulani然后制作
ImageView
android:scaleType=“fitXY”
public class SquareImageView extends ImageView {
    public SquareImageView(Context context) {
        super(context);
    }

    public SquareImageView(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public SquareImageView(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
    }

    @Override
    public void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        int width = MeasureSpec.getSize(widthMeasureSpec);
        int height = MeasureSpec.getSize(heightMeasureSpec);
        View rootView = this.getRootView();
        if (rootView != null) {
            width = rootView.getWidth();
            height = rootView.getHeight();
        }
        int size = width > height ? height : width;
        super.onMeasure(size, size);
        setMeasuredDimension(size, size);
    }
}