android布局

android的布局我们常用的有四种,

(1)LinearLayout  线性布局

    LinearLayout-用于在单行或单列中显示子视图控件。用于创建 表单来说是非常方便的布局方法。线性布局分为两种:水平方向和垂直方向的布局。分别通过属性:Android:orientation=‘vertical'和Android:orientation=“horizontal”来设置。Android:layout_weight表示 子元素占据的空间大小的比例。

(2)TableLayout 表格布局

表格布局,适用于多行多列的布局格式,每个TableLayout是由多个TableRow组成,一个TableRow就表示TableLayout中的每一行,这一行可以由多个子元素组成。实际上TableLayout和TableRow都是LineLayout线性布局的子类。但是TableRow的参数android:orientation属性值固定为horizontal,且android:layout_width=MATCH_PARENT,android:layout_height=WRAP_CONTENT。所以TableRow实际是一个横向的线性布局。

注意:在TableLayout中,单元格可以为空,但是不能跨列,意思是不能有相邻的单元格为空。

在TableLayout布局中,一列的宽度由该列中最宽的那个单元格指定,而该表格的宽度由父容器指定。可以为每一列设置以下属性:

     Shrinkable  表示该列的宽度可以进行收缩,以使表格能够适应父容器的大小

     Stretchable 表示该列的宽度可以进行拉伸,以使能够填满表格中的空闲空间

     Collapsed  表示该列会被隐藏

TableLayout中的特有属性:

   android:collapseColumns

      android:shrinkColumns

      android:stretchColumns = "0,1,2,3"

 

(3)RelativeLayout 相对布局

   RelativeLayout继承于android.widget.ViewGroup,其按照子元素之间的位置关系完成布局的,作为Android系统五大布局中最灵活也是最常用的一种布局方式,非常适合于一些比较复杂的界面设计。

   注意:在引用其他子元素之前,引用的ID必须已经存在,否则将出现异常。

常用的位置属性:

android:layout_toLeftOf  该组件位于引用组件的左方

android:layout_toRightOf 该组件位于引用组件的右方

android:layout_above 该组件位于引用组件的上方

android:layout_below     该组件位于引用组件的下方

android:layout_alignParentLeft  该组件是否对齐父组件的左端

android:layout_alignParentRight 该组件是否齐其父组件的右端

android:layout_alignParentTop   该组件是否对齐父组件的顶部

android:layout_alignParentBottom  该组件是否对齐父组件的底部

android:layout_centerInParent   该组件是否相对于父组件居中

android:layout_centerHorizontal   该组件是否横向居中

android:layout_centerVertical   该组件是否垂直居中

 

(4)FrameLayout 框架布局

 将所有的子元素放在整个界面的左上角,后面的子元素直接覆盖前面的子元素,所以用的比较少。

 

(5) AbsoluteLayou 绝对布局

   绝对布局中将所有的子元素通过设置android:layout_x 和 android:layout_y属性,将子元素的坐标位置固定下来,即坐标(android:layout_x, android:layout_y) ,layout_x用来表示横坐标,layout_y用来表示纵坐标。 屏幕左上角为坐标(0,0),横向往右为正方,纵向往下为正方。实际应用中,这种布局用的比较少,因为Android终端一般机型比较多,各自的屏幕大小,分辨率等可能都不一样,如果用绝对布局,可能导致在有的终端上显示不全等。

 

(6)GridLayout网格布局

   使用虚细线将布局划分为行,列和单元格,同时也支持在行,列上进行交错排列一般适合显示图标、图片等内容。继承与ViewGroup。常用的属性如下:

Attribute Name

Related Method

Description

android:alignmentMode

setAlignmentMode(int) 

当设置alignMargins,使视图的外边界之间进行校准,定义其边距,当设置alignBounds,使视图的边界之间进行校准,默认设置alignMargins

android:columnCount

setColumnCount(int) 

自动定位子视图时创建的最大列数 

android:columnOrderPreserved

setColumnOrderPreserved(boolean) 

当设置为true,使列边界显示的顺序和列索引的顺序相同。默认是true

android:orientation

setOrientation(int) 

The orientation property is not used during layout. 

android:rowCount

setRowCount(int) 

自动定位子视图时创建的最大行数

android:rowOrderPreserved

setRowOrderPreserved(boolean) 

当设置为true,使行边界显示的顺序和行索引的顺序相同。默认是true 

android:useDefaultMargins

setUseDefaultMargins(boolean) 

当设置ture,当没有指定视图的布局参数时,告诉GridLayout使用默认的边距。默认值是false 

 

布局优化:填充布局是一个巨大的开销的过程,每个额外的嵌套布局和它包含的View都直接影响应用程序的性能和响应能力。为了使应用程序流畅地运行尽可能的保持布局的简单和避免出现因为相对较小UI的变动而完全填充新的布局的情况。

一般,布局嵌套数量限制在少于10层,布局包含View的个数不应该超过80.

 

除了上面的几种布局,下面列出了常用的布局属性的介绍

(1)layout_margin  

用于设置控件边缘相对于父控件的边距 

android:layout_marginLeft  

android:layout_marginRight 

android:layout_marginTop 

android:layout_marginBottom 

 

(2) layout_padding  

用于设置控件内容相对于控件边缘的边距 

android:layout_paddingLeft 

android:layout_paddingRight 

android:layout_paddingTop 

android:layout_paddingBottom 

 

(3) layout_width/height 

用于设置控件的高度和宽度 

wrap_content 内容包裹,表示这个控件的里面文字大小填充 

fill_parent 跟随父窗口 

match_parent

(4)gravity的中文意思就是”重心“,就是表示view横向和纵向的停靠位置

android:gravity:是对view控件本身来说的,是用来设置view本身的内容应该显示在view的什么位置,默认值是左侧。

android:layout_gravity:是相对于包含改元素的父元素来说的,设置该元素在父元素的什么位置

比如TextView: android:layout_gravity表示TextView在界面上的位置,android:gravity表示TextView文本在TextView的什么位置,默认值是左侧.

基本选项值:

Value

Description

top

Put the object at the top of its container, not changing its size.
将对象放在其容器的顶部,不改变其大小.

bottom

Put the object at the bottom of its container, not changing its size.
将对象放在其容器的底部,不改变其大小.

left

Put the object at the left edge of its container, not changing its size.
将对象放在其容器的左侧,不改变其大小.

right

Put the object at the right edge of its container, not changing its size.
将对象放在其容器的右侧,不改变其大小.

center_vertical

Place object in the vertical center of its container, not changing its size.
将对象纵向居中,不改变其大小.
垂直对齐方式:垂直方向上居中对齐。

fill_vertical

Grow the vertical size of the object if needed so it completely fills its container.
必要的时候增加对象的纵向大小,以完全充满其容器.
垂直方向填充

center_horizontal

Place object in the horizontal center of its container, not changing its size.
将对象横向居中,不改变其大小.
水平对齐方式:水平方向上居中对齐

fill_horizontal

Grow the horizontal size of the object if needed so it completely fills its container.
必要的时候增加对象的横向大小,以完全充满其容器.
水平方向填充

center

Place the object in the center of its container in both the vertical and horizontal axis, not changing its size.
将对象横纵居中,不改变其大小.

fill

Grow the horizontal and vertical size of the object if needed so it completely fills its container. This is the default.
必要的时候增加对象的横纵向大小,以完全充满其容器.

clip_vertical

Additional option that can be set to have the top and/or bottom edges of the child clipped to its container's bounds. The clip is based on the vertical gravity: a top gravity clips the bottom edge, a bottom gravity clips the top edge, and neither clips both edges.

附加选项,用于按照容器的边来剪切对象的顶部和/或底部的内容剪切基于其纵向对齐设置:顶部对齐时,剪切底部;底部对齐时剪切顶部;除此之外剪切顶部和底部.

垂直方向裁剪

clip_horizontal

Additional option that can be set to have the left and/or right edges of the child clipped to its container's bounds. The clip is based on the horizontal gravity: a left gravity clips the right edge, a right gravity clips the left edge, and neither clips both edges.

附加选项,用于按照容器的边来剪切对象的左侧和/或右侧的内容剪切基于其横向对齐设置:左侧对齐时,剪切右侧;右侧对齐时剪切左侧;除此之外剪切左侧和右侧.

水平方向裁剪

 


  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值