【问题标题】:GridView android 75x75 exact screen size of squareGridView android 75x75 正方形的确切屏幕尺寸
【发布时间】:2013-05-26 14:11:08
【问题描述】:

我想制作一个包含 75dp 宽度和 75dp 高度的图像视图的 GridView。而且我希望这个正方形填充屏幕大小,中间没有空格。现在我只使用 500 计数。所以它随机打印500个正方形图像。它没有填满空间。请查看下图了解平板电脑版本。

这是我的适配器:

    public class ImageAdapter extends BaseAdapter {
    private Context mContext;

    public ImageAdapter(Context c) {
        mContext = c;
    }

    public int getCount() {
        return 500;
    }

    public Object getItem(int position) {
        return null;
    }

    public long getItemId(int position) {
        return 0;
    }

    // create a new ImageView for each item referenced by the Adapter
    public View getView(int position, View convertView, ViewGroup parent) {


        ImageView imageView;
        if (convertView == null) { // if it's not recycled, initialize some
            // attributes
            imageView = new ImageView(mContext);
            imageView.setLayoutParams(new GridView.LayoutParams(75, 75));
            imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
            imageView.setPadding(0, 0, 0, 0);
        } else {
            imageView = (ImageView) convertView;
        }

        imageView.setImageResource(R.drawable.sample_0);
        return imageView;
    }

}

【问题讨论】:

  • 您希望其中的 500 个图像填满屏幕,或者您希望尽可能多的 75 x 75 像素图像填满整个屏幕?无论哪种情况,您都需要获取显示高度和宽度,然后进行一些计算。
  • 你想达到什么目的?是什么原因把所有五颜六色的视图都放了?
  • 我希望 75x75 填满屏幕。我正在使用游戏。

标签: android android-layout gridview


【解决方案1】:

getCount 中,您可以将屏幕面积除以您要填充的方块的面积。

public int getCount() {
    Display display = getWindowManager().getDefaultDisplay();
    Point size = new Point();
    display.getSize(size);
    int area = size.x * size.y;
    // TODO: you may want to pass in the square dimensions or make them constants
    return area / (75 * 75);
}

当然,这是不对的,因为它使用的是显示而不是父 ViewGroup。与其尝试在适配器中完成这一切,不如让构造函数获取父级的尺寸并在那里进行数学运算,保存一个带有计数的实例变量:

private int numBoxes = 0;
private static final SQUARE_SIZE = 75;
public ImageAdapter(Context c, ViewGroup parent) {
    mContext = c;
    numBoxes = (parent.getWidth() * parent.getHeight()) / (SQUARE_SIZE * SQUARE_SIZE);
}
public int getCount() {
    return numBoxes;
}

【讨论】:

  • 嗨尼克,谢谢。我试过后者。当我运行代码时。它不打印任何东西。 numBoxes = (parent.getWidth() * parent.getHeight()) / (SQUARE_SIZE * SQUARE_SIZE);看起来像返回 0
【解决方案2】:

扩展 Nick White 的答案,您需要确保父布局已布局,然后才能获得高度和宽度。如果父宽度和/或高度是“wrap_content”或“match_parent”,如果适配器是在 Activity onCreate() 中创建的,则 getHeight() 和 getWidth() 将在适配器的构造函数中返回零。

answer 提供了一种侦听 onLayout 事件和处理大小调整的方法。我确实尝试过做类似的事情,但我发现我的结果并不完美,因为在我应用大小更改后网格会绘制然后重绘。到目前为止,我在调整网格大小方面获得的最佳结果是使用合格的资源值来调整网格内的图像视图大小。然后通过在 values-xxx 资源文件夹中创建各种值来根据屏幕密度或大小设置资源大小值。

在我的例子中,我通过应用以下内容来更改单元格图像的大小以使某个数字适合屏幕:

整数大小 = getResources().getInteger(R.int.image_size));

imageView.setLayoutParams(new GridView.LayoutParams(size, size));

应该可以做类似的事情来计算屏幕大小的列数。

【讨论】:

    猜你喜欢
    • 2013-02-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-01-05
    • 1970-01-01
    • 1970-01-01
    • 2023-04-05
    • 1970-01-01
    相关资源
    最近更新 更多