zoukankan      html  css  js  c++  java
  • android LayoutParams 简单说明 理解 示例

       简单说说 自己对 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.   /**<TextView 
    10.            android:layout_width="fill_parent" 
    11.            android:layout_height="wrap_content" 
    12.            android:text="Text View"/>*/  
    13. //在xml里边怎么配置高宽大家都会的。  
    14.   //第一个参数为宽的设置,第二个参数为高的设置。  
    15.            LinearLayout.LayoutParams p = new LinearLayout.LayoutParams(     
    16.                    LinearLayout.LayoutParams.FILL_PARENT,     
    17.                    LinearLayout.LayoutParams.WRAP_CONTENT     
    18.            );     
    19.            //调用addView()方法增加一个TextView到线性布局中  
    20.            mLayout.addView(textView, p);     
    21.           //比较简单的一个例子  

    如果还不能理解下边在来一段直白的说明: 
    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,即包裹住组件就好。 
  • 相关阅读:
    jQuery 操作元素属性、内容、css、class
    jQuery 选择器
    JavaScript 二级菜单(含过渡动画)
    tools.js
    JavaScript JSON
    中华人民共和国国务院令第644号 国务院关于修改《全国年节及纪念日放假办法》的决定
    Linux系统Yum仓库制作
    玩转Linux系统之常用命令
    IDEA安装插件提示was not installed: Cannot download解决办法
    软件开发过程中常用的环境解释DEV FAT UAT PRO
  • 原文地址:https://www.cnblogs.com/wlh652475101/p/3489121.html
Copyright © 2011-2022 走看看