Android: can height of SlidingDrawer be set with wrap_content?

后端 未结 6 2101
花落未央
花落未央 2020-11-27 10:38

I\'m trying to implement a SlidingDrawer that will occupy the full screen width, but whose height is determined dynamically by its contents: in other words, sta

6条回答
  •  心在旅途
    2020-11-27 11:15

    The onMeasure() method of the SlidingDrawer class basically overrides the layout modes to fill_parent, this is why layout_height="wrap_content" is not working.

    To get around this, you can extend SlidingDrawer with a re-implemented onMeasure() method that honors the layout_width and layout_height attributes. You can then use this custom class in your XML layout by replacing with .

    Note that since the drawer will no longer be filling the parent layout, you will have to enclose it in a LinearLayout with the gravity attribute set to the edge where the drawer should be.

    Below are a class I have created for this purpose and an example layout.

    WrappingSlidingDrawer class :

    import android.content.Context;
    import android.util.AttributeSet;
    import android.view.View;
    import android.widget.SlidingDrawer;
    
    
    public class WrappingSlidingDrawer extends SlidingDrawer {
    
        public WrappingSlidingDrawer(Context context, AttributeSet attrs, int defStyle) {
            super(context, attrs, defStyle);
    
            int orientation = attrs.getAttributeIntValue("android", "orientation", ORIENTATION_VERTICAL);
            mTopOffset = attrs.getAttributeIntValue("android", "topOffset", 0);
            mVertical = (orientation == SlidingDrawer.ORIENTATION_VERTICAL);
        }
    
        public WrappingSlidingDrawer(Context context, AttributeSet attrs) {
            super(context, attrs);
    
            int orientation = attrs.getAttributeIntValue("android", "orientation", ORIENTATION_VERTICAL);
            mTopOffset = attrs.getAttributeIntValue("android", "topOffset", 0);
            mVertical = (orientation == SlidingDrawer.ORIENTATION_VERTICAL);
        }
    
        @Override
        protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
    
            int widthSpecMode = MeasureSpec.getMode(widthMeasureSpec);
            int widthSpecSize =  MeasureSpec.getSize(widthMeasureSpec);
    
            int heightSpecMode = MeasureSpec.getMode(heightMeasureSpec);
            int heightSpecSize =  MeasureSpec.getSize(heightMeasureSpec);
    
            if (widthSpecMode == MeasureSpec.UNSPECIFIED || heightSpecMode == MeasureSpec.UNSPECIFIED) {
                throw new RuntimeException("SlidingDrawer cannot have UNSPECIFIED dimensions");
            }
    
            final View handle = getHandle();
            final View content = getContent();
            measureChild(handle, widthMeasureSpec, heightMeasureSpec);
    
            if (mVertical) {
                int height = heightSpecSize - handle.getMeasuredHeight() - mTopOffset;
                content.measure(widthMeasureSpec, MeasureSpec.makeMeasureSpec(height, heightSpecMode));
                heightSpecSize = handle.getMeasuredHeight() + mTopOffset + content.getMeasuredHeight();
                widthSpecSize = content.getMeasuredWidth();
                if (handle.getMeasuredWidth() > widthSpecSize) widthSpecSize = handle.getMeasuredWidth();
            }
            else {
                int width = widthSpecSize - handle.getMeasuredWidth() - mTopOffset;
                getContent().measure(MeasureSpec.makeMeasureSpec(width, widthSpecMode), heightMeasureSpec);
                widthSpecSize = handle.getMeasuredWidth() + mTopOffset + content.getMeasuredWidth();
                heightSpecSize = content.getMeasuredHeight();
                if (handle.getMeasuredHeight() > heightSpecSize) heightSpecSize = handle.getMeasuredHeight();
            }
    
            setMeasuredDimension(widthSpecSize, heightSpecSize);
        }
    
        private boolean mVertical;
        private int mTopOffset;
    }
    

    Example layout (assuming WrappingSlidingDrawer is in package com.package) :

    
        ... stuff you want to cover at full-size ...
        
            
                ... handle and content views ...
            
        
    
    

提交回复
热议问题