自定义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);    }}

在布局中引入就可以了

更多相关文章

  1. Android(安卓)DataBinding ViewPage + Fragment 封装
  2. Android(安卓)数据库SQLite的使用简单Demo
  3. android 网络连接 HttpGet HttpPost方法
  4. 关于 android AES 部分机器 javax.crypto.BadPaddingException:
  5. Android添加room依赖的正确姿势(附带完整流程)
  6. Android(安卓)强制竖屏
  7. android 传递图片和图片与byte的转换
  8. Android(安卓)dialog 对话框自定义布局
  9. 收集到的Android开源代码

随机推荐

  1. Android适用于IM通知音频的SoundPool
  2. android实现图片按任意路径截取
  3. 系出名门Android(7) - 控件(View)之ZoomC
  4. android src: eclipse 导入 android Sett
  5. Animation中多段动画的连续播放
  6. Android短信功能实现源码
  7. android make
  8. Android系统的Binder机制之四——系统Ser
  9. Android(安卓)app 与 ActivityManagerSer
  10. [Android] 你真的了解Activity吗?