【Tips】recycleview加载指定比例长宽的Item布局

2016-12-22  本文已影响275人  最爱平角裤

recycle加载item的时候有时候会要求item的长宽成一定比例

主要通过重写RelativeLayout

public class SquareItem extends RelativeLayout {
    public SquareItem(Context context) {
        super(context);
    }

    public SquareItem(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public SquareItem(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        setMeasuredDimension(getDefaultSize(0, widthMeasureSpec),
                getDefaultSize(0, heightMeasureSpec));
        int childWidthSize = getMeasuredWidth();
        // 高度和宽度一样

        widthMeasureSpec = MeasureSpec.makeMeasureSpec(
                childWidthSize, MeasureSpec.EXACTLY);
        
        heightMeasureSpec = childWidthSize/6*5;  //设定高是宽的比例
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
    }
}
上一篇下一篇

猜你喜欢

热点阅读