如何强制 GridView 在 Android 中生成方形单元格 [英] How to force GridView to generate square cells in Android

查看:13
本文介绍了如何强制 GridView 在 Android 中生成方形单元格的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何强制android GridView生成方形单元格(单元格的高度等于单元格的宽度)GridView 有 10*9 个单元格,应用必须支持多屏!

How can I force an android GridView to generate square cells (Cell's height equal to cell's width) GridView has 10*9 cells and the app must support multiple screens !

我使用了线性布局:

row_grid.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:padding="0dp" >

        <ImageView
            android:id="@+id/item_image"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:scaleType="fitXY"
            android:src="@drawable/ic_launcher" >
        </ImageView>

    </LinearLayout>

和 GridView:activity_main.xml

and GridView: activity_main.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@drawable/katy" >

    <GridView
        android:id="@+id/gridView1"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:horizontalSpacing="0dp"
        android:numColumns="4"
        android:stretchMode="columnWidth"
        android:verticalSpacing="0dp" >
    </GridView>
</RelativeLayout>

GridView 的每个单元格都包含一个具有相同宽度和高度的图像.图像比单元格大,必须拉伸以适应单元格.

Each cell of GridView contains a image that has same width and height. images are larger than cells and must stretch to fit in cells.

推荐答案

首先,您需要创建一个自定义 View 类,您可以使用该类代替您正在使用的默认 LinearLayout.然后你想覆盖视图的 onMeasure 调用,并强制它是方形的:

First, you're going to want to create a custom View class that you can use instead of the default LinearLayout you're using. Then you want to override the View's onMeasure call, and force it to be square:

public class GridViewItem extends ImageView {

    public GridViewItem(Context context) {
        super(context);
    }

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

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

    @Override
    public void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, widthMeasureSpec); // This is the key that will make the height equivalent to its width
    }
}

然后您可以将 row_grid.xml 文件更改为:

Then you can change your row_grid.xml file to:

<path.to.item.GridViewItem xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/item_image"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:scaleType="centerCrop"
    android:src="@drawable/ic_launcher" >
</path.to.item.GridViewItem>

请务必将path.to.item"更改为您的 GridViewItem.java 类所在的包.

Just be sure to change "path.to.item" to the package where your GridViewItem.java class resides.

我还删除了您的 LinearLayout 父级,因为它没有为您做任何事情.

I also removed your LinearLayout parent since it wasn't doing anything for you there.

还将 scaleType 从 fitXY 更改为 centerCrop 以便您的图像不会自行拉伸并保持其纵横比.而且,只要是方形图像,无论如何都不应裁剪任何内容.

Also changed scaleType from fitXY to centerCrop so that your image doesn't stretch itself and maintains its aspect ratio. And, as long as it's a square image, nothing should be cropped, regardless.

这篇关于如何强制 GridView 在 Android 中生成方形单元格的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆