Android(安卓)GridView设置条目的高度与宽度相等、GridView条目宽高相同
16lz
2021-01-24
自定义View,继承RelativeLayout或者LinearLayout,重写onMeasure()方法,代码如下:
public class SquareLayout extends RelativeLayout { public SquareLayout(Context context, AttributeSet attrs, int defStyle) { super(context, attrs, defStyle); } public SquareLayout(Context context, AttributeSet attrs) { super(context, attrs); } public SquareLayout(Context context) { super(context); } protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { setMeasuredDimension(getDefaultSize(0, widthMeasureSpec), getDefaultSize(0, heightMeasureSpec)); int childWidthSize = getMeasuredWidth(); int childHeightSize = getMeasuredHeight(); heightMeasureSpec = widthMeasureSpec = MeasureSpec.makeMeasureSpec(childWidthSize, MeasureSpec.EXACTLY); super.onMeasure(widthMeasureSpec, heightMeasureSpec); }}
在布局中引入就可以了
更多相关文章
- Android(安卓)DataBinding ViewPage + Fragment 封装
- Android(安卓)数据库SQLite的使用简单Demo
- android 网络连接 HttpGet HttpPost方法
- 关于 android AES 部分机器 javax.crypto.BadPaddingException:
- Android添加room依赖的正确姿势(附带完整流程)
- Android(安卓)强制竖屏
- android 传递图片和图片与byte的转换
- Android(安卓)dialog 对话框自定义布局
- 收集到的Android开源代码