demo示例layout
? XML版本=' 1.0 '编码=' utf-8 '? androidx.constraint layout .构件. constraintlayoutxmlns 3360 Android=' http://schemas.Android.com/apk/RES/安卓id _ constraint _ layout '安卓3360 layout _ width=' match _ parent '安卓3360 layout _ height=' match _ width
private void initView ()//创建imageviewimageviewimageview=new imageview ) ) this ); @suppresslint(resourcetype ) ) inputstream inputstream=get resources ).openrawresource (r.mi pmap.BG ); bitmap bitmap=bitmap factory.decode stream (输入流; imageview.setimagebitmap; 将ImageView转换为constraintlayoutconstraintlayout=findviewbyid (r.id.id _ constraint _ layout ); constraint layout.add view (imageview; ImageView布局参数constraint layout.layoutparamsparams=newconstraintlayout.layout params (imageview.getlayoutparams ) params.topToTop=parentId; params.bottomToBottom=parentId; params.leftToLeft=parentId; params.rightToRight=parentId; imageview.setlayoutparams (params; }注意: ImageView的父布局是ConstraintLayout,因此使用ConstraintLayout.LayoutParams设置布局参数。
如果ImageView的父布局是线性布局,则布局参数必须使用线性布局.布局参数。
其他父布局也是如此。