简单说说 自己对 android LayoutParams的理解吧,xh写不出高级文章是低级写手。
public static class
ViewGroup.LayoutParams
extends Object

java.lang.Object
   ↳ android.view.ViewGroup.LayoutParams   //继承关系

以下说明摘自官方文档E文好的可以看看
Class Overview

LayoutParams are used by views to tell their parents how they want to be laid out. See ViewGroup Layout Attributes for a list of all child view attributes that this class supports.

The base LayoutParams class just describes how big the view wants to be for both width and height. For each dimension, it can specify one of:

FILL_PARENT (renamed MATCH_PARENT in API Level 8 and higher), which means that the view wants to be as big as its parent (minus padding)
WRAP_CONTENT, which means that the view wants to be just big enough to enclose its content (plus padding)
an exact number
There are subclasses of LayoutParams for different subclasses of ViewGroup. For example, AbsoluteLayout has its own subclass of LayoutParams which adds an X and Y value.

E文不好看不懂  但是觉得写得啰嗦了
其实这个LayoutParams类是用于child view(子视图) 向 parent view(父视图)传达自己的意愿的一个东西(孩子想变成什么样向其父亲说明)其实子视图父视图可以简单理解成
一个LinearLayout 和 这个LinearLayout里边一个 TextView 的关系 TextView 就算LinearLayout的子视图 child view 。需要注意的是LayoutParams只是ViewGroup的一个内部类这里边这个也就是ViewGroup里边这个LayoutParams类是 base class 基类实际上每个不同的ViewGroup都有自己的LayoutParams子类
比如LinearLayout 也有自己的 LayoutParams 大家打开源码看几眼就知道了
myeclipse 怎么查看源码 请看http://byandby.iteye.com/blog/814277
下边来个例子

Java代码 :
  1. //创建一个线性布局
  2. private LinearLayout mLayout;
  3. mLayout = (LinearLayout) findViewById(R.id.layout);
  4. //现在我要往mLayout里边添加一个TextView
  5. //你可能会想直接在布局文件里边配置不就O 了 那是 但是这里为了说明问题我们用代码实现
  6. TextView textView = new TextView(Activity01.this);
  7. textView.setText("Text View " );
  8. //这里请不要困惑这里是设置 这个textView的布局 FILL_PARENT WRAP_CONTENT 和在xml文件里边设置是一样的如
  9. //在xml里边怎么配置高宽大家都会的。
  10. //第一个参数为宽的设置,第二个参数为高的设置。
  11. LinearLayout.LayoutParams p = new LinearLayout.LayoutParams(
  12. LinearLayout.LayoutParams.FILL_PARENT,
  13. LinearLayout.LayoutParams.WRAP_CONTENT
  14. );
  15. //调用addView()方法增加一个TextView到线性布局中
  16. mLayout.addView(textView, p);
  17. //比较简单的一个例子

如果还不能理解下边在来一段直白的说明:
LayoutParams继承于Android.View.ViewGroup.LayoutParams.
LayoutParams相当于一个Layout的信息包,它封装了Layout的位置、高、宽等信息。假设在屏幕上一块区域是由一个Layout占领的,如果将一个View添加到一个Layout中,最好告诉Layout用户期望的布局方式,也就是将一个认可的layoutParams传递进去。
可以这样去形容LayoutParams,在象棋的棋盘上,每个棋子都占据一个位置,也就是每个棋子都有一个位置的信息,如这个棋子在4行4列,这里的“4行4列”就是棋子的LayoutParams。
但LayoutParams类也只是简单的描述了宽高,宽和高都可以设置成三种值:
1,一个确定的值;
2,FILL_PARENT,即填满(和父容器一样大小);
3,WRAP_CONTENT,即包裹住组件就好。

关于setLayoutParams报错

 

  在继承BaseAdapter的时候,用getView返回View的时候,用代码控制布局,需要用到View.setLayoutParams,但是报错了,报的是类型转换错误,经过研究,发现,这里不能使用ViewGroup.LayoutParams而必须使用对应父View的LayoutParams类型。如:某View被LinearLayout包含,则该View的setLayoutParams参数类型必须是LinearLayout.LayoutParams。原因在于LinearLayout(或其他继承自ViewGroup的layout,如:RelativeLayout)在进行递归布局的时候,LinearLayout会获取子View的LayoutParams,并强制转换成LinearLayout.LayoutParams,如

1 LinearLayout.LayoutParams lp = (LinearLayout.LayoutParams) child.getLayoutParams();

或者是如下定义:

1 LayoutParams lp = (LayoutParams) child.getLayoutParams();

以转换成内部类型LinearLayout.LayoutParams。

Android中LayoutParams的用法的更多相关文章

  1. Android 中 LayoutParams 的用法

    一个控件应当使用它的父控件的 LayoutParams 类型.因此,一个 TableVow 应该使用 TableLayout.Params . 所以,以一个 TableRow 为例: TableRow ...

  2. android 中uri.parse()用法

    android 中uri.parse()用法 1,调web浏览器 Uri myBlogUri = Uri.parse("http://xxxxx.com"); returnIt = ...

  3. Android中Selector的用法(改变ListView和Button的默认背景)

    Android中的Selector的用法 http://blog.csdn.net/shakespeare001/article/details/7788400#comments Android中的S ...

  4. Android中Intent的用法总结

    Intent只在Android中特有,我把它比作一种运载工具,就像飞机一样,会把一些人带到某个地方,而且如果需要的话,还可以找到机上有哪些人员(数据),这就需要另外一些设备来支持(如:Bundle), ...

  5. android中sharedPreferences的用法

    SharedPreferences介绍:   做软件开发应该都知道,很多软件会有配置文件,里面存放这程序运行当中的各个属性值,由于其配置信息并不多,如果采用数据库来存放并不划算,因为数据库连接跟操作等 ...

  6. 【转】Android中Application类用法

    转自:http://www.cnblogs.com/renqingping/archive/2012/10/24/Application.html Application类 Application和A ...

  7. Android中Application类用法

    Application类 Application和Activity,Service一样是Android框架的一个系统组件,当Android程序启动时系统会创建一个Application对象,用来存储系 ...

  8. 【Android】Android 中string-array的用法

    在Android中,用string-array是一种简单的提取XML资源文件数据的方法. 例子如下: 把相应的数据放到values文件夹的arrays.xml文件里 <?xml version= ...

  9. (转)Android中Parcelable接口用法

    1. Parcelable接口 Interface for classes whose instances can be written to and restored from a Parcel. ...

随机推荐

  1. Fragment生命周期(转)

    Android在3.0中引入了fragments的概念,主要目的是用在大屏幕设备上--例如平板电脑上,支持更加动态和灵活的UI设计.平板电脑的屏幕要比手机的大得多,有更多的空间来放更多的UI组件,并且 ...

  2. 网页设计编辑利器——jQuery EasyUI所学整理(待编辑)

    1, Messager弹窗信息 方法: $.messager.alert(...), 在网页中间弹出一个窗口 $.messager.confirm(...) 弹出一个确认窗口, 有确定和取消两个按钮, ...

  3. win bat命令后台运行控制台应用

    @echo off if "%1"=="h" goto begin start mshta vbscript:createobject()(window.clo ...

  4. Linux基石【第二篇】虚拟网络三种连接方式(转载)

    在虚拟机上安装完Centos系统后,开始配置静态IP,以方便在本宿主机上可以访问虚拟机,在曲折的配置中,了解到虚拟机还有三种连接方式:Bridged,NAT和Host-only,于是,我又一轮新的各种 ...

  5. APP测试功能点总结

    1.功能性测试:   ——根据产品需求文档编写测试用例. ——软件设计文档编写用例.注意:就是根据产品需求文档编写测试用例而进行测试. 2.兼容性测试:   ——android版本的兼容性 ——手机分 ...

  6. centos7 搭建svn服务

    linux(centos)下SVN服务器如何搭建?说到SVN服务器,想必大家都知道,可以是在LINUX下如何搭建SVN服务器呢?那么今天给大家分享一下linux(centos)搭建SVN服务器的思路! ...

  7. 第十届国际用户体验创新大赛携Mockplus走进校园

    今日立夏,万木并秀,生生不息,第十届国际用户体验创新大赛即将拉开序幕.5月5日下午,一场用户体验设计经验分享活动携带众多嘉宾降临成都理工大学,为西南赛区首站赛事宣讲. 本次宣讲活动邀请了华为技术有限公 ...

  8. jquery.validate remote 和 自定义验证方法

    jquery.validate remote 和 自定义验证方法 $(function(){ var validator = $("#enterRegForm").validate ...

  9. [转]Android下怎么使用LDD查看依赖库

    Android下没有ldd可以使用,在进行ndk开发的时候,检查库的依赖项特别麻烦.有两个解决方案: 1.将linux的的ldd移植过去.因为android也是基于linux的,所以将ldd移植过去是 ...

  10. JQuery中button提交表单报TypeError: elem[type] is not a function jquery

    错误: TypeError: elem[type] is not a function jquery 解决: 出现这种现象的原因是,提交的表单中,有标签的name,有以submit命名的 name中不 ...