如何强制GridView控件生成多细胞的Andr​​oid [英] How to force GridView to generate square cells in Android

查看:170
本文介绍了如何强制GridView控件生成多细胞的Andr​​oid的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我怎么能强迫一个Android的GridView产生方形单元(Cell的高度等于单元格的宽度) 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.

推荐答案

首先,你将要创建一个自定义视图,您可以使用,而不是默认的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控件生成多细胞的Andr​​oid的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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