《Android开发自我整理知识——Android布局》

来源:互联网 发布:win7优化性能xp 编辑:程序博客网 时间:2024/04/29 07:07

Android开发中View的布局方式一般有如下几种:

1、LinearLayout (线性布局)

线性布局作为最常用的布局之一,一般分为两种形式,横向线性布局和纵向线性布局;即其子控件View在水平方向上或者垂直方向上线性排列分布,但不便于修改控件的显示位置,开发中经常与相对布局配合嵌套使用。

基本属性如下:

android:background       设置整个布局画面的背景
android:orientation="horizontal"        子元素的排列队形,是横向排列,还是纵向排列
android:gravity="bottom"          子元素在布局中的缺省看齐方式
android:padding       设置子元素与布局边缘之间的空白
android:layout_gravity          设置自身对象在父布局中的看齐方式,可以更新父布局对象gravity属性给出的缺省属性
android:layout_weight          将父布局中剩余的尺寸按各兄弟元素的weight值比例进行填充。
android:layout_margin          设置自身对象边缘与父布局的边缘之间的空白

<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent">     <TextView  android:layout_width="fill_parent" android:layout_height="wrap_content" android:background="#ff000000" android:text="@string/hello"/>     <LinearLayout android:orientation="horizontal" android:layout_width="fill_parent" android:layout_height="fill_parent">         <TextView  android:layout_width="fill_parent" android:layout_height="wrap_content" android:background="#ff654321" android:layout_weight="1" android:text="1"/>         <TextView  android:layout_width="fill_parent" android:layout_height="wrap_content" android:background="#fffedcba" android:layout_weight="2"  android:text="2"/>     </LinearLayout> </LinearLayout>

2、RelativeLayout(相对布局

相对布局作为最常用的布局之一,属性较多和具备FrameLayout帧布局特性的优势使其可以基本适配任何UI界面

第一类:属性值为true或false 
android:layout_centerHrizontal          水平居中 
android:layout_centerVertical            垂直居中 
android:layout_centerInparent          相对于父元素完全居中 
android:layout_alignParentBottom           贴紧父元素的下边缘 
android:layout_alignParentLeft          贴紧父元素的左边缘 
android:layout_alignParentRight        贴紧父元素的右边缘 
android:layout_alignParentTop          贴紧父元素的上边缘 
android:layout_alignWithParentIfMissing        如果对应的兄弟元素找不到的话就以父元素做参照物 

第二类:属性值必须为id的引用名“@id/id-name” 
android:layout_below                在某元素的下方 
android:layout_above          在某元素的的上方 
android:layout_toLeftOf       在某元素的左边 
android:layout_toRightOf           在某元素的右边 

android:layout_alignTop       本元素的上边缘和某元素的的上边缘对齐 
android:layout_alignLeft       本元素的左边缘和某元素的的左边缘对齐 
android:layout_alignBottom        本元素的下边缘和某元素的的下边缘对齐 
android:layout_alignRight           本元素的右边缘和某元素的的右边缘对齐 

第三类:属性值为具体的像素值,如30dip,40px 
android:layout_marginBottom           离某元素底边缘的距离 
android:layout_marginLeft          离某元素左边缘的距离 
android:layout_marginRight         离某元素右边缘的距离 
android:layout_marginTop          离某元素上边缘的距离

<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent">     <TextView android:id="@+id/text_01" android:layout_width="50dp" android:layout_height="50dp" android:background="#ffffffff" android:gravity="center" android:layout_alignParentBottom="true" android:text="1"/>     <TextView android:id="@+id/text_02" android:layout_width="50dp" android:layout_height="50dp" android:background="#ff654321" android:gravity="center" android:layout_above="@id/text_01" android:layout_centerHorizontal="true" android:text="2"/>     <TextView android:id="@+id/text_03" android:layout_width="50dp" android:layout_height="50dp" android:background="#fffedcba" android:gravity="center" android:layout_toLeftOf="@id/text_02" android:layout_above="@id/text_01" android:text="3"/> </RelativeLayout>

3、FrameLayout(帧布局)

帧布局作为最简单布局之一,直接在屏幕上开辟除了一块空白区域。原理是指控件中绘制的任何一个控件都可以被后绘制的控件覆盖,最后绘制的控件会遮盖之前的控件,可以理解为叠加层布局。

android:foreground 设置该帧布局容器的前景图像

android:foregroundGravity设置前景图像显示的位置

<?xml version="1.0" encoding="utf-8"?> <FrameLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent">     <TextView android:layout_width="fill_parent" android:layout_height="fill_parent" android:background="#ff000000" android:gravity="center" android:text="1"/>     <TextView android:layout_width="fill_parent" android:layout_height="fill_parent" android:background="#ff654321" android:gravity="center" android:text="2"/>     <TextView android:layout_width="50dp" android:layout_height="50dp" android:background="#fffedcba" android:gravity="center" android:text="3"/> </FrameLayout>

4、TableLayout(表格布局)

表格布局,顾名思义,通过行列的表格形式来管理UI组建,布局中不需要明确的声明行列条数,通过容器TableRow及相关组建来控制表格的行列条数。

android:collapseColumns     setColumnsCollapsed(in,boolean)   设置需要隐藏的列的序号,多个用逗号隔

android:shrinkColumns       setShrinkColumns(boolean)         设置需要收缩的列的序号,多个用逗号隔

android:stretchColumns      setStretchAllColumns(boolean)     设置需要伸张的列的序号,多个用逗号隔

android:layout_column        指定单元格在第几列显示

android:layout_span          指定单元格占的列数(未指定时 为1)

android:collapse=“1”因此该TableLayout里的TableRow的列1,即第二列(从0开始计算)

说明:收缩和伸张的属性可以同时使用,stretchColumns是将父控件填满,属性值是从0开始,0代表第一列

      行数和列数的确定:是通过TableRow以及其他组件控制表格的行数和列数,具体如下:

      TableLayout的行数有开发人员制定,即有多少个TableRow对象(或view控件),就有多少行

      列的宽度:由该列中最宽的单元格决定,整个表格布局的宽度取决于父容器的宽度(默认是占满父容器本身)  如第一个TableRow含2个控件,第二个TableRow含3个控件,那么该TableRow的列数为3


<?xml version="1.0" encoding="utf-8"?> <TableLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent">     <TableRow android:layout_width="fill_parent" android:layout_height="wrap_content">         <TextView  android:background="#ffffffff" android:gravity="center" android:padding="10dp" android:text="1"/>         <TextView android:background="#ff654321" android:gravity="center" android:padding="10dp" android:text="2"/>         <TextView  android:background="#fffedcba" android:gravity="center" android:padding="10dp" android:text="3"/>     </TableRow>     <TableRow android:layout_width="fill_parent" android:layout_height="wrap_content">         <TextView  android:background="#ff654321" android:gravity="center" android:padding="10dp" android:text="2"/>         <TextView android:background="#fffedcba" android:gravity="center" android:padding="10dp" android:text="3"/>             </TableRow>     <TableRow android:layout_width="fill_parent" android:layout_height="wrap_content">         <TextView android:background="#fffedcba" android:gravity="center" android:padding="10dp" android:text="3"/>         <TextView  android:background="#ff654321" android:gravity="center" android:padding="10dp" android:text="2"/>         <TextView  android:background="#ffffffff" android:gravity="center" android:padding="10dp" android:text="1"/>             </TableRow> </TableLayout>

5、AbsoluteLayout(绝对布局

绝对布局可以设置控件在屏幕中的X、Y坐标点,和帧布局一样后绘制的控件会覆盖之前绘制的控件,但是Android的手机分辨率五花八门使得绝对布局在部分其它分辨率的手机上无法正常显示,官方API中已废弃使用。

6.其他布局(隶属关系请看上图)

1)列表视图(List View)

  List View是可滚动的列表。以列表的形式展示具体内容,并且能够根据数据的长度自适应显示。

      具体应用请看:用法一  http://www.cnblogs.com/allin/archive/2010/05/11/1732200.html

         用法二  http://blog.csdn.net/koupoo/article/details/7018727 

2)网格视图(Grid View)

  Grid View一个ViewGroup以网格显示它的子视图(view)元素,即二维的、滚动的网格。

  具体应用查看:http://www.cnblogs.com/linzheng/archive/2011/01/19/1938760.html

3)标签布局(Tab Layout)

  以标签的方式显示它的子视图元素,就像在Firefox中的一个窗口中显示多个网页一样。为了狂创建一个标签UI(tabbed UI),需要使用到TabHost和TabWidget。TabHost必须是布局的根节点,它包含为了显示标签的TabWidget和显示标签内容的FrameLayout。

  具体应用查看:http://www.cnblogs.com/devinzhang/archive/2012/01/18/2325887.html

参考文献:博客园http://www.cnblogs.com/devinzhang/archive/2012/01/19/2327535.html
              51CTOhttp://xys289187120.blog.51cto.com/3361352/656986



0 0
原创粉丝点击