Android: can height of SlidingDrawer be set with wrap_content?

后端 未结 6 2098
花落未央
花落未央 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:34

    it is better to read the parameter without hardcoding the string:

        int attrOrientation = android.R.attr.orientation;
        int attrTopOffset = android.R.attr.topOffset;
    
        int[] attrIds = new int [] {attrOrientation, attrTopOffset}; 
    
        TypedArray a = context.obtainStyledAttributes(attrs, attrIds);
        int orientation = a.getInt(0, SlidingDrawer.ORIENTATION_VERTICAL);
        topOffset = a.getDimension(1, 0);
        a.recycle(); 
    
        isVertical = (orientation == SlidingDrawer.ORIENTATION_VERTICAL);
    

    Another issus is in the onMeasure.

    I used the following code:

        if (isVertical) {
            int height = heightSpecSize - handle.getMeasuredHeight() - topOffset;
            getContent().measure(MeasureSpec.makeMeasureSpec(widthSpecSize, MeasureSpec.UNSPECIFIED), MeasureSpec.makeMeasureSpec(height, MeasureSpec.EXACTLY));
            heightSpecSize = handle.getMeasuredHeight() + topOffset + content.getMeasuredHeight();
            widthSpecSize = content.getMeasuredWidth();
            if (handle.getMeasuredWidth() > widthSpecSize) widthSpecSize = handle.getMeasuredWidth();
        } else {
            int width = widthSpecSize - handle.getMeasuredWidth() - topOffset;
            getContent().measure(MeasureSpec.makeMeasureSpec(width, MeasureSpec.EXACTLY), MeasureSpec.makeMeasureSpec(heightSpecSize, MeasureSpec.UNSPECIFIED));
            widthSpecSize = handle.getMeasuredWidth() + topOffset + content.getMeasuredWidth();
            heightSpecSize = content.getMeasuredHeight();
            if (handle.getMeasuredHeight() > heightSpecSize) heightSpecSize = handle.getMeasuredHeight();
        }
    

提交回复
热议问题