自定义GridLayout动态添加View,且平分宽高。
注意:这不是最好的方法,只是我研究出一种不靠谱的方法。
1.导入兼容包
compile 'com.android.support:gridlayout-v7:22.+'
2.设置最大的行和列(注意app:)
app:rowCount="4"
app:columnCount="4"
3.动态添加子View
LayoutParams params = new LayoutParams(
GridLayout.spec(GridLayout.UNDEFINED, 1f),
GridLayout.spec(GridLayout.UNDEFINED, 1f));//1f为比重
params.width=mWidth/4;
params.height=mWidth/4;
addView(blockView,params);
4.如果需要子View为正方形,修改子View的onMeasure方法
mWidth = MeasureSpec.getSize(widthMeasureSpec);
setMeasuredDimension(mWidth, mWidth);
5.GridLayout子类平分<