.LayoutParams 介绍

.LayoutParams  其实就是一个Layout信息容器,装了 Layout的宽,高的信息,告诉他们的父容器,他们想放在哪,多大

下面是2种用法:

setLayoutParams(new TableRow.LayoutParams(TableRow.LayoutParams.FILL_PARENT,TableRow.LayoutParams.FILL_PARENT));


FrameLayout.LayoutParams lytp = new FrameLayout.LayoutParams(80,LayoutParams.WRAP_CONTENT);
lytp .gravity = Gravity.CENTER;
btn.setLayoutParams(lytp);

 可以这样去形容LayoutParams,在象棋的棋盘上,每个棋子都占据一个位置,也就是每个棋子都有一个位置的信息,如这个棋子在4行4列,这里的“4行4列”就是棋子的LayoutParams。


       但LayoutParams类也只是简单的描述了宽高,宽和高都可以设置成三种值:
       1,一个确定的值;
       2,FILL_PARENT,即填满(和父容器一样大小);
       3,WRAP_CONTENT,即包裹住组件就好。


下面是谷歌原文:

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.

Developer Guides

For more information about creating user interface layouts, read the XML Layouts developer guide.

Summary


XML Attributes
Attribute Name Related Method Description
android:layout_height   Specifies the basic height of the view. 
android:layout_width   Specifies the basic width of the view. 
Constants
int FILL_PARENT Special value for the height or width requested by a View.
int MATCH_PARENT Special value for the height or width requested by a View.
int WRAP_CONTENT Special value for the height or width requested by a View.
Fields
public int height Information about how tall the view wants to be.
public  LayoutAnimationController.AnimationParameters layoutAnimationParameters Used to animate layouts.
public int width Information about how wide the view wants to be.
Public Constructors
ViewGroup.LayoutParams( Context c,  AttributeSet attrs)
Creates a new set of layout parameters.
ViewGroup.LayoutParams(int width, int height)
Creates a new set of layout parameters with the specified width and height.
ViewGroup.LayoutParams( ViewGroup.LayoutParams source)
Copy constructor.
Protected Methods
void setBaseAttributes( TypedArray a, int widthAttr, int heightAttr)
Extracts the layout parameters from the supplied attributes.
[Expand]
Inherited Methods
 From class java.lang.Object

XML Attributes


android:layout_height

Specifies the basic height of the view. This is a required attribute for any view inside of a containing layout manager. Its value may be a dimension (such as "12dip") for a constant height or one of the special constants.

May be a dimension value, which is a floating point number appended with a unit such as "14.5sp". Available units are: px (pixels), dp (density-independent pixels), sp (scaled pixels based on preferred font size), in (inches), mm (millimeters).

This may also be a reference to a resource (in the form "@[package:]type:name") or theme attribute (in the form "?[package:][type:]name") containing a value of this type.

May be one of the following constant values.

Constant Value Description
fill_parent -1 The view should be as big as its parent (minus padding). This constant is deprecated starting from API Level 8 and is replaced by match_parent.
match_parent -1 The view should be as big as its parent (minus padding). Introduced in API Level 8.
wrap_content -2 The view should be only big enough to enclose its content (plus padding).

This corresponds to the global attribute resource symbol layout_height.

Related Methods
    android:layout_width

    Specifies the basic width of the view. This is a required attribute for any view inside of a containing layout manager. Its value may be a dimension (such as "12dip") for a constant width or one of the special constants.

    May be a dimension value, which is a floating point number appended with a unit such as "14.5sp". Available units are: px (pixels), dp (density-independent pixels), sp (scaled pixels based on preferred font size), in (inches), mm (millimeters).

    This may also be a reference to a resource (in the form "@[package:]type:name") or theme attribute (in the form "?[package:][type:]name") containing a value of this type.

    May be one of the following constant values.

    Constant Value Description
    fill_parent -1 The view should be as big as its parent (minus padding). This constant is deprecated starting from API Level 8 and is replaced by match_parent.
    match_parent -1 The view should be as big as its parent (minus padding). Introduced in API Level 8.
    wrap_content -2 The view should be only big enough to enclose its content (plus padding).

    This corresponds to the global attribute resource symbol layout_width.

    Related Methods

      Constants


      public static final int FILL_PARENT
      Since:  API Level 1

      Special value for the height or width requested by a View. FILL_PARENT means that the view wants to be as big as its parent, minus the parent's padding, if any. This value is deprecated starting in API Level 8 and replaced by MATCH_PARENT.

      Constant Value: -1 (0xffffffff)
      public static final int MATCH_PARENT
      Since:  API Level 8

      Special value for the height or width requested by a View. MATCH_PARENT means that the view wants to be as big as its parent, minus the parent's padding, if any. Introduced in API Level 8.

      Constant Value: -1 (0xffffffff)
      public static final int WRAP_CONTENT
      Since:  API Level 1

      Special value for the height or width requested by a View. WRAP_CONTENT means that the view wants to be just large enough to fit its own internal content, taking its own padding into account.

      Constant Value: -2 (0xfffffffe)

      Fields


      public int height
      Since:  API Level 1

      Information about how tall the view wants to be. Can be one of the constants FILL_PARENT (replaced by MATCH_PARENT , in API Level 8) or WRAP_CONTENT. or an exact size.

      public LayoutAnimationController.AnimationParameters layoutAnimationParameters
      Since:  API Level 1

      Used to animate layouts.

      public int width
      Since:  API Level 1

      Information about how wide the view wants to be. Can be one of the constants FILL_PARENT (replaced by MATCH_PARENT , in API Level 8) or WRAP_CONTENT. or an exact size.

      Public Constructors


      public ViewGroup.LayoutParams (Context c, AttributeSet attrs)
      Since:  API Level 1

      Creates a new set of layout parameters. The values are extracted from the supplied attributes set and context. The XML attributes mapped to this set of layout parameters are:

      Parameters
      c the application environment
      attrs the set of attributes from which to extract the layout parameters' values
      public ViewGroup.LayoutParams (int width, int height)
      Since:  API Level 1

      Creates a new set of layout parameters with the specified width and height.

      Parameters
      width the width, either WRAP_CONTENTFILL_PARENT (replaced by MATCH_PARENT in API Level 8), or a fixed size in pixels
      height the height, either WRAP_CONTENTFILL_PARENT (replaced by MATCH_PARENT in API Level 8), or a fixed size in pixels
      public ViewGroup.LayoutParams (ViewGroup.LayoutParams source)
      Since:  API Level 1

      Copy constructor. Clones the width and height values of the source.

      Parameters
      source The layout params to copy from.

      Protected Methods


      protected void setBaseAttributes (TypedArray a, int widthAttr, int heightAttr)
      Since:  API Level 1

      Extracts the layout parameters from the supplied attributes.

      Parameters
      a the style attributes to extract the parameters from
      widthAttr the identifier of the width attribute
      heightAttr the identifier of the height attribute

      评论
      添加红包

      请填写红包祝福语或标题

      红包个数最小为10个

      红包金额最低5元

      当前余额3.43前往充值 >
      需支付:10.00
      成就一亿技术人!
      领取后你会自动成为博主和红包主的粉丝 规则
      hope_wisdom
      发出的红包
      实付
      使用余额支付
      点击重新获取
      扫码支付
      钱包余额 0

      抵扣说明:

      1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
      2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

      余额充值