【问题标题】:Android: How to make a gridview of equal proportions within the Java codeAndroid:如何在 Java 代码中制作等比例的网格视图
【发布时间】:2014-04-02 09:45:57
【问题描述】:

我正在开发一个具有网格视图布局的主屏幕的应用程序。 就目前而言,我在 DP 数字中硬编码盒子的高度和宽度,我显然不想这样做。如何创建代码来确定用户的屏幕尺寸并相应地匹配框,以便拥有大手机(如便笺)的人会看到与 droid mini 上的人相同的东西? IE,当每边硬编码为 500dp 时,它看起来像这样:

但我的目标是让它在每个屏幕尺寸上看起来都像这样:

目前,这是我拥有的 Java 代码:

公共类 ActivityAdapter 扩展 BaseAdapter {

//Array of Icons that will be used as menu choices
public static int[] imageOptions = {
        R.drawable.vacation_quota, //Position 0
        R.drawable.revenue_deficit, //Position 1
            //+ many more options...
    };

private Context context;

//Constructor to pass context back to Main class
public ActivityAdapter(Context applicationContext) {

    context = applicationContext;
}

//Number of elements to be displayed on the grid
@Override
public int getCount() {

    return imageOptions.length;
}

@Override
public View getView(int position, View convertView, ViewGroup arg2) {
    ImageView iv;
    if(convertView != null){
        iv = (ImageView) convertView;
    } else {
        iv = new ImageView(context);

                    //Here I have it hard coded to match a 480px width. Here is where I need to change it, just not sure how
        iv.setLayoutParams(new GridView.LayoutParams(240, 240));

        iv.setScaleType(ScaleType.CENTER_CROP); //Center the cropping

    }

    //Sets the image to correspond to its position within the array.
    iv.setImageResource(imageOptions[position]);
    return iv;
}

有什么想法吗?我想从逻辑上讲我想弄清楚屏幕尺寸并使用它们来使图像尺寸匹配,只是不确定如何。其他线程的研究没有太大帮助,所以希望我可以通过代码发布在这里得到一个更简单的答案。

【问题讨论】:

  • 只有一种......这里的代码无法正常工作,因为我无法在活动类之外完成这项工作,并且我试图避免改变应用程序的基本结构。我最终只提出了一个简单的建议:iv = new ImageView(context); WindowManager wm = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);显示显示 = wm.getDefaultDisplay();浮动宽度2 = display.getWidth(); int 长度 = (int) (width2/2); iv.setLayoutParams(new GridView.LayoutParams(length, length));

标签: android gridview grid imageview baseadapter


【解决方案1】:

你应该得到屏幕的宽度和高度。然后使用这些值来动态设置每个单元格的高度和宽度

【讨论】:

  • 我同意甘德,这实际上就是我在这个问题中要问的问题。
  • 使用`getContext().getResources().getDisplayMetrics()`然后得到宽度和高度
  • 当我使用'getContext().getResources().getDisplayMetrics();'它提出了“未定义为主要类型”。我究竟如何使用它?它是否设置为等于变量?我需要创建一个对象才能使用它吗?
  • 是的;将其分配给 DisplayMetrics 对象并使用 widthPixels 和 heigthPixels 获取尺寸
【解决方案2】:

试试这个代码-

public class MainActivity extends Activity {

    @Override
    protected void onCreate(Bundle state) {
        setContentView(new ViewGroup(this) {
            private RelativeLayout[] items = new RelativeLayout[9];
            private int width, height, itemWidth, itemHeight;
            {
                Random r = new Random();
                for (int i = 0; i < 9; i++) {
                    items[i] = new RelativeLayout(getContext());
                    float[] hsv = new float[] {360 * r.nextFloat(), .50f, .75f};
                    items[i].setBackgroundColor(Color.HSVToColor(hsv));
                    addView(items[i]);

                    // UPDATE ////////////////////////////////////
                    ImageView image = new ImageView(getContext());
                    switch (i) {
                    case 0: // top left
                    case 1: // top center
                    case 2: // top right
                    case 3: // center left
                    case 4: // center center
                    case 5: // center right
                    case 6: // bottom left
                    case 7: // bottom center
                    case 8: // bottom right
                        image.setImageResource(R.drawable.ic_launcher);
                        break;
                    }
                    image.setScaleType(ScaleType.FIT_XY);
                    image.setLayoutParams(new RelativeLayout.LayoutParams(
                            RelativeLayout.LayoutParams.MATCH_PARENT,
                            RelativeLayout.LayoutParams.MATCH_PARENT
                    ));
                    items[i].addView(image);
                    //////////////////////////////////////////////
                }
            }
            @Override
            protected void onMeasure(int wMS, int hMS) {
                width = MeasureSpec.getSize(wMS);
                height = MeasureSpec.getSize(hMS);
                itemWidth = width / 3;
                itemHeight = height / 3;
                wMS = MeasureSpec.makeMeasureSpec(itemWidth, MeasureSpec.EXACTLY);
                hMS = MeasureSpec.makeMeasureSpec(itemHeight, MeasureSpec.EXACTLY);
                measureChildren(wMS, hMS);
                setMeasuredDimension(width, height);
            }
            @Override
            protected void onLayout(boolean changed, int l, int t, int r, int b) {
                for (int i = 0; i < 9; i++) {
                    l = itemWidth * (i % 3);
                    t = itemHeight * (i / 3);
                    r = l + itemWidth;
                    b = t + itemHeight;
                    items[i].layout(l, t, r, b);
                }
            }
        });
        super.onCreate(state);
    }

}

输出-

编辑-

要创建可滚动的网格视图,请参阅http://www.androidhive.info/2012/02/android-gridview-layout-tutorial/

【讨论】:

  • 只需复制粘贴并检查输出。然后根据您的要求修改代码。
  • Kanwaljit,感谢您如此彻底的回复!我在尝试使代码为我工作时遇到了一些麻烦。首先,我将拥有超过 9 个项目,所以我试图弄清楚如何将其设为允许用户向下滚动并在下面查看更多选项的滚动视图。其次,为了让这些图片可以点击到他们打开一个活动的地方,我要把那个代码放在哪里?
【解决方案3】:

我最终无法使用此处的一些建议,不得不使用已弃用的方法来保留我的应用程序结构。具体添加到我的代码是:

        iv = new ImageView(context);

        WindowManager wm = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
        Display display = wm.getDefaultDisplay();

        float width2 = display.getWidth();

        int length = (int) (width2/2);

        iv.setLayoutParams(new GridView.LayoutParams(length, length));  

这样,高度和宽度将根据屏幕大小裁剪到正确的大小。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-12-29
    • 1970-01-01
    • 1970-01-01
    • 2019-12-13
    • 2022-11-03
    相关资源
    最近更新 更多