`
byandby
  • 浏览: 1688537 次
  • 性别: Icon_minigender_1
  • 来自: 北京
社区版块
存档分类
最新评论

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
下边来个例子
       //创建一个线性布局
        private LinearLayout mLayout;   
        mLayout = (LinearLayout) findViewById(R.id.layout);   
       //现在我要往mLayout里边添加一个TextView 
      //你可能会想直接在布局文件里边配置不就O 了 那是 但是这里为了说明问题我们用代码实现
       TextView textView = new TextView(Activity01.this);   
            textView.setText("Text View " );
            //这里请不要困惑这里是设置 这个textView的布局 FILL_PARENT WRAP_CONTENT 和在xml文件里边设置是一样的如
   /**<TextView
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:text="Text View"/>*/
 //在xml里边怎么配置高宽大家都会的。
   //第一个参数为宽的设置,第二个参数为高的设置。
            LinearLayout.LayoutParams p = new LinearLayout.LayoutParams(   
                    LinearLayout.LayoutParams.FILL_PARENT,   
                    LinearLayout.LayoutParams.WRAP_CONTENT   
            );   
            //调用addView()方法增加一个TextView到线性布局中
            mLayout.addView(textView, p);   
           //比较简单的一个例子

如果还不能理解下边在来一段直白的说明:
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,即包裹住组件就好。

基本就这么多了,不要想的太复杂。
分享到:
评论
6 楼 ZSRTFAT 2012-12-01  
5 楼 djb2006005 2012-02-27  
xh写不出高级文章是低级写手..写的很好,很易懂的。
4 楼 only_xxp 2011-08-10  
学习了, 讲得很明白
3 楼 foreverwey 2011-07-30  
很好,学习了
2 楼 海阔天空一路是蓝 2011-03-19  
不错,come on!谢谢为社区做的贡献哦!嘿嘿
1 楼 王路喜 2011-03-09  
谢谢,茅塞顿开

相关推荐

    Android LinearLayout.LayoutParams 使用方法

    1.LinearLayout.LayoutParams使用说明 2.在Java代码中利用LinearLayout.LayoutParams构建布局

    LayoutParams类

    LayoutParams类描述

    Android View的六种移动方式

    自定义view的六种移动方式,通过使用offsetLeftAndRight(offsetX),setLayoutParams(layoutParams)等等,还可以控制view的滚动速度

    一个全屏显示的Android注册界面示例

    一个简单的Android注册界面,在注册时支持全屏显示,通过下列两句实现:getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);隐去状态栏部分电池等图标...

    WindowManager.LayoutParams

    在此文档中列出了 所有WindowManager.LayoutParams的参数及详细信息

    WindowManager属性详解

    WindowManager属性详解:WindowManager.LayoutParams 是 WindowManager 接口的嵌套类;继承于 ViewGroup.LayoutParams 。 它的内容十分丰富。其实WindowManager.java的主要内容就是由这个类定义构成。

    Android中悬浮窗口的实现原理和示例代码

    调用WindowManager,并设置WindowManager.LayoutParams的相关属性,通过WindowManager的addView方法创建View

    android - 交换控件(relativelayout)位置,LayoutParams与ObjectAnimator两种方法

    现需要交换两个控件...1、使用LayoutParams改变两个layout的属性,即其相对关系(below等),实现位置的交换,但是并没有交换的动画效果,是“瞬间”交换。 2、使用animation交换控件位置,实现了我需要的动画效果。

    android播放gif格式图片示例

    代码如下:import android.content.Context;...import android.view.ViewGroup.LayoutParams; import com.nmbs.R;   public class GifView extends View { private long movieStart; private Movie

    LayoutParams

    LayoutParams

    android 实现FlowLayout 流线布局(自定义ViewGroup)

    [快速理解android View的测量onMeasure()与MeasureSpec](http://blog.csdn.net/double2hao/article/details/51553703) ###2、Layoutparams以及MarginLayoutParams [Android开发:LayoutParams的用法]...

    Android开发艺术探索.任玉刚(带详细书签).pdf

    本书是一本Android进阶类书籍,采用理论、源码和实践相结合的方式来阐述高水准的Android应用开发要点。本书从三个方面来组织内容。第一,介绍Android开发者不容易掌握的一些知识点;第二,结合Android源代码和应用层...

    Android 悬浮窗功能实现(微信语音通话悬浮窗效果实现)

    2.代码示例 3.实现效果及便捷工具类 4.仿微信语音通话悬浮窗效果实现 4.1 需求分析及效果展示  4.2 实现 5.最后 1.基本介绍 Android 界面绘制都是通过 WindowManager 服务来实现的,WindowManager 对象可通过获取 ...

    android window说明

    android 各种类型窗口说明,各种类型LayoutParams 各種Type值介紹 .

    谷歌开源的Android排版库 FlexboxLayout.zip

    FlexboxLayout 是 Android 上实现了类似 CSS Flexible Box Layout Module 效果的界面排版。示例代码:FlexboxLayout flexboxLayout = (FlexboxLayout) findViewById(R.id.flexbox_layout); flexboxLayout....

    android开发入门与实战(下)

    7.1.4 布局参数(LayoutParams) 7.2 我的美丽我做主——Android中应用界面布局 7.2.1 实例操作演示 7.2.2 实例编程实现 7.3 不积跬步无以至千里——常用widget组件介绍 7.3.1 创建widget组件实例 7.3.2 按钮(Button)...

    android 用户界面UI详解

    android 用户界面 UI 布局 ,LayoutParams ,

    Android代码-android-UCToast

    睡不着起不来的万先生 的 Android无需权限显示悬浮窗, 兼谈逆向分析app 文中提到,type 为 WindowManager.LayoutParams.TYPE_TOAST 的 WindowManager.LayoutParam 无需权限,即可让 View 显示。 本项目模拟实现该...

    android图片转换器示例

    import android.app.ActionBar.LayoutParams;import android.app.Activity;import android.os.Bundle;import android.view.View;import android.view.View.OnClickListener;import android.view.animation....

    Android入门到精通源代码.

    《Android从入门到精通》 第1章 初识Android 1.1 什么是Android 1.1.1 移动信息设备分类 1.1.2 Open Handset Alliance和 Android 1.2 Android简介 1.2.1 Andriod的历史 1.2.2 Andriod的未来 1.2.3 Andriod平台的技术...

Global site tag (gtag.js) - Google Analytics