首页 > 编程知识 正文

layout教程(getLayoutParams)

时间:2023-05-04 11:30:47 阅读:69627 作者:338

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的父布局是线性布局,则布局参数必须使用线性布局.布局参数。

其他父布局也是如此。

版权声明:该文观点仅代表作者本人。处理文章:请发送邮件至 三1五14八八95#扣扣.com 举报,一经查实,本站将立刻删除。