how to set image width height based on screen size in gridview

后端 未结 3 1881
旧时难觅i
旧时难觅i 2021-02-04 09:00

I want to show 3x3 sized gridview. I want to set the height and width based on device size. I am taking reference from this link.

MainActivity-

public cl         


        
3条回答
  •  一个人的身影
    2021-02-04 09:27

    Do not use screen size, in a multi-windows context this method is invalid. If your grid is a 3x3 items size fixed, so use custom layout ViewGroup like this: (and set RelativeLayout items content)

    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);
        }
    
    }
    

    EDIT : see my update in code, you have simply to add your images to the items containers. With this method, no XML layout file needed because you manage content and size yourself.

    Result : result


    EDIT : the minimalist way:

    public class MainActivity extends Activity {
    
        @Override
        protected void onCreate(Bundle state) {
            setContentView(new ViewGroup(this) {
                private static final int SIZE_X = 3;
                private static final int SIZE_Y = 3;
                private ImageView[] items = new ImageView[SIZE_X * SIZE_Y];
                private int itemWidth, itemHeight;
                {
                    setBackgroundColor(Color.DKGRAY);
                    for (int i = 0; i < items.length; i++) {
                        items[i] = new ImageView(getContext());
                        items[i].setScaleType(ScaleType.CENTER);
                        items[i].setImageResource(R.drawable.ic_launcher);
                        addView(items[i]);
                    }
                }
                @Override
                protected void onMeasure(int wMS, int hMS) {
                    int width = MeasureSpec.getSize(wMS);
                    int height = MeasureSpec.getSize(hMS);
                    itemWidth = width / SIZE_X;
                    itemHeight = height / SIZE_Y;
                    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 < items.length; i++) {
                        l = itemWidth * (i % SIZE_X);
                        t = itemHeight * (i / SIZE_X);
                        r = l + itemWidth;
                        b = t + itemHeight;
                        items[i].layout(l, t, r, b);
                    }
                }
            });
            super.onCreate(state);
        }
    
    }
    

    Result :

    enter image description here


    @ Kanwaljit Singh:

    In MainActivity items creation loop:

    final int id = i;
    items[i].setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View v) {
            startActivity(new Intent(getContext(), NextActivity.class).putExtra("id", id));
        }
    });
    

    In NextActivity:

    int id = getIntent().getIntExtra("id", -1);
    

提交回复
热议问题