博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
【Android】不知道的 setLayoutParams
阅读量:7106 次
发布时间:2019-06-28

本文共 1014 字,大约阅读时间需要 3 分钟。

hot3.png

动态创建Layout的时候,LayoutParams 是必须要设置的参数,关于 setLayoutParams 知道多少呢?

  1. setLayoutParams 是设给父节点的。 文档里说的清楚:

Set the layout parameters associated with this view. These supply parameters to the parent of this view specifying how it should be arranged. There are many subclasses of ViewGroup.LayoutParams, and these correspond to the different subclasses of ViewGroup that are responsible for arranging their children.

也就是说这个setLayoutParams()是给其父控件看的,只有父类可以改变子View的位置布局,而不是说子View可以随意设置。

  1. setLayoutParams 类型不能随便给。 清楚了LayoutParams是根据父节点来设置的,虽然使用的时候创建一个 ViewGroup.LayoutParams 也能够设置成功,但还是要根据父节点的类型来定义为具体的 RelativeLayout.LayoutParams、LinearLayout.LayoutParams等,否则运行的时候会抛出异常的。

    <!-- lang: java -->

    RelativeLayout.LayoutParams layoutParams= new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);

    <!-- lang: java -->

    layoutParams.addRule(RelativeLayout.CENTER_IN_PARENT);

    <!-- lang: java -->

    view.setLayoutParams(layoutParams);

转载于:https://my.oschina.net/giszhang/blog/323075

你可能感兴趣的文章
管理CentOS常用指令
查看>>
23种设计模式的有趣见解
查看>>
Biztalk学习笔记1
查看>>
JavaScript入门篇 第二天(消息对话框+网页弹出)
查看>>
Redis Cluster
查看>>
[Reading]THIRD BASE
查看>>
高效上网教程---如何高效率收集高质量的信息
查看>>
php课程 6-21 HTML标签相关函数
查看>>
html5如何实现元素拖放
查看>>
Font Awesome 图标如何使用
查看>>
javascript创建对象的方法--工厂模式(非常好理解)
查看>>
amazeui页面分析4
查看>>
day9--paramiko模块
查看>>
多线程编程学习一(Java多线程的基础)
查看>>
CentOS6.5 升级 Python 2.7 版本
查看>>
lvs -dr
查看>>
亚马逊S3 - The difference between the request time and the current time is too large.
查看>>
问答:制造业转运营
查看>>
as3+java+mysql(mybatis) 数据自动工具(二)
查看>>
Hdu 4864(Task 贪心)(Java实现)
查看>>