动态创建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等,否则运行的时候会抛出异常的。

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

    layoutParams.addRule(RelativeLayout.CENTER_IN_PARENT);

    view.setLayoutParams(layoutParams);

更多相关文章

  1. Android写sys文件节点
  2. JS C# 获取浏览器类型
  3. android 经纬度 double类型 转换成 度分秒
  4. android webview加载String类型html
  5. 基于xml类型的压缩数据流的android获取天气的方法
  6. 【转】Android -- 探究Android的多分辨率支持以及各种类型图标尺
  7. 利用gson处理Java反射TypeToken泛型运行时类型擦除

随机推荐

  1. android手机之间用wifi direct通信
  2. [ 转]Android(安卓)监控网络状态
  3. Android(安卓)Intent用法详解
  4. 解决 android.content.ActivityNotFoundE
  5. Android(安卓)NetworkCallback
  6. Android(安卓)Opencore OpenMAX学习(1)
  7. Android(安卓)读取Assets中图片
  8. 操作Android(安卓)手机通讯录数据库
  9. android环境配置
  10. Android入门三 Http通信(实例)