ImageView taking too much vertical space

后端 未结 4 778
Happy的楠姐
Happy的楠姐 2021-01-11 22:55

i am working on a project with a staggered gridview that can support varying height for each nested view,


           


        
4条回答
  •  失恋的感觉
    2021-01-11 23:19

    I had a similar problem and I resolved it by overriding onMeasure method of RelativeLayout.

    package com.etsy.android.grid.util;
    
    import android.content.Context;
    import android.util.AttributeSet;
    import android.widget.RelativeLayout;
    
    public class DynamicHeightRelativeLayout extends RelativeLayout {
    
        public DynamicHeightRelativeLayout(Context context) {
            super(context);
        }
    
        public DynamicHeightRelativeLayout(Context context, AttributeSet attrs) {
            super(context, attrs);
        }
    
        public DynamicHeightRelativeLayout(Context context, AttributeSet attrs, int defStyle) {
            super(context, attrs, defStyle);
        }
    
        @Override
        protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
    
            // Hack - This causes the height of RelativeLayout to match
            //        it's content when RelativeLayout is shown in StaggeredGridView. 
            heightMeasureSpec = MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED);
    
            super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        }
    
    }
    

    Then I used this class in XML layout file:

    
    
    
        
    
        
    
        
    
    
    

提交回复
热议问题