1、LayoutParams
错误方法
ImageView imageview = new ImageView(this);
ViewGroup.LayoutParams lp = imageview.getLayoutParams(); //得到的lp拿来使用,空指针异常
lp.width = ViewGroup.LayoutParams.MATCH_PARENT;
lp.height = ViewGroup.LayoutParams.MATCH_PARENT;
imageview.setLayoutParams(lp);
imageview.setScaleType(ImageView.ScaleType.FIT_XY);
正确方法:
ImageView imageview = new ImageView(this);
ViewGroup.LayoutParams lp = new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
lp.width = ViewGroup.LayoutParams.MATCH_PARENT;
lp.height = ViewGroup.LayoutParams.MATCH_PARENT;
imageview.setLayoutParams(lp);
imageview.setScaleType(ImageView.ScaleType.FIT_XY);