android中如何设置ImageView的宽高比?

在iOS中可以设置一个控件的宽高比,比如说一个ImageView的宽设置为屏幕的宽度,高等于宽度,即这个ImageView是正方形,而且适配所有机型。
在Android的xml怎么设置呢?

阅读 11.3k
2 个回答
新手上路,请多包涵

重写一个view extends Imageview,然后

  protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        int width = MeasureSpec.getSize(widthMeasureSpec);
        int height = Math.round(width*ratio);
        setMeasuredDimension(width,height);
    }

这个ratio设置成1

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题