当GridView嵌套在其他布局中,我们会发现有GridView只显示一行的情况,如果将高度给一个固定值,则可以显示,但这不是我们想要的,解决方案如下:
自定义GridView,重写onMeasure方法
直接使用以下自定义类即可
/**
* author SGY
* date 2019/7/1
* desc
*/
public class AllShowGridView extends GridView {
public AllShowGridView(Context context) {
super(context);
}
public AllShowGridView(Context context, AttributeSet attrs) {
super(context, attrs);
}
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
int expandSpec = MeasureSpec.makeMeasureSpec(Integer.MAX_VALUE >> 2, MeasureSpec.AT_MOST);
super.onMeasure(widthMeasureSpec, expandSpec);
}
}