Android RecyclerView 网格显示正方形

使用recyclerView显示纵向滑动的网格列表,只需要设置

mListView.setLayoutManager(new GridLayoutManager(context,4));

public class MyLayout extends RelativeLayout {

public  MyLayout  (Context context, AttributeSet attrs,
                            int defStyle) {
    super(context, attrs, defStyle);
}

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

public MyLayout (Context context) {
    super(context);
}

@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
    setMeasuredDimension(getDefaultSize(0, widthMeasureSpec),
            getDefaultSize(0, heightMeasureSpec));

    int childWidthSize = getMeasuredWidth();
    // 高度和宽度一样
    heightMeasureSpec = widthMeasureSpec = MeasureSpec.makeMeasureSpec(
            childWidthSize, MeasureSpec.EXACTLY);
    super.onMeasure(widthMeasureSpec, heightMeasureSpec);
}

}

おすすめ

転載: blog.csdn.net/xieyaofeng/article/details/101430060