GridLayout Column width

前端 未结 6 2008
再見小時候
再見小時候 2021-02-11 12:27

I have 2 columns in my GridLayout. What I want to do is make those columns take up half of the width of the screen each and then have its child contents fill their

6条回答
  •  春和景丽
    2021-02-11 13:19

    You could extend the RelativeLayout class (or use LinearLayout if you want to) to make sure the height of the item will be the same as the height.

    public class GridItem extends RelativeLayout {
    
            public GridItem(Context context) {
                    super(context);
            }
    
            public GridItem(Context context, AttributeSet attr) {
                    super(context, attr);
            }
    
            public GridItem(Context context, AttributeSet attr, int integer) {
                    super(context, attr, integer);
            }
    
            // Override onMeasure to give the view the same height as the specified width
            @Override
            public void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
                super.onMeasure(widthMeasureSpec, widthMeasureSpec);
                setMeasuredDimension(getMeasuredWidth(), getMeasuredWidth());
            }
    
    }
    

    The parent view of the item layout should be the GridItem view to make sure it works. This must be the layout file you'll inflate in the getView of your ListAdapter

    
    
    
        
    
    
    

    And set the stretchMode of the GridView to columnWidth. This will make all items fit to the width specified number of columns. The new view will make sure they will also have the same height.

    
    

提交回复
热议问题