android五大布局

http://blog.csdn.net/eyebrows_cs/article/details/7831679

http://blog.csdn.net/zj0078/article/details/7390505

Android 平台定义了5种布局类型

线性布局(linear layout)

表格布局(table layout)

绝对布局(absolute layout)

相对布局(relative layout)

框布局(frame layout)

( 一 ) LinearLayout

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:paddingLeft="16dp"
    android:paddingRight="16dp"
    android:orientation="vertical" >
    <EditText
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:hint="to" />
    <EditText
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:hint="subject" />
    <EditText
        android:layout_width="fill_parent"
        android:layout_height="0dp"
        android:layout_weight="1"
        android:gravity="top"
        android:hint="message" />
    <Button
        android:layout_width="100dp"
        android:layout_height="wrap_content"
        android:layout_gravity="right"
        android:text="send" />
</LinearLayout>

属性解释:

android:orientation : 设置方向 垂直(vertical)或水平(horizontal)

android:layout_width/layout_height: 设置控件 宽度 或高度

android:layout_gravity: 控件右对齐

android:gravity : 控件内容对齐方式

android:hint: EditText编辑提示文字

 

(二)RelativeLayout

Tips:  不能有一个交叉依赖对方位置的布局;

例如,你不能有一个RelativeLayout高度设置 WRAP_CONTENT和一个子集,以 ALIGN_PARENT_BOTTOM的

 

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:paddingLeft="16dp"
    android:paddingRight="16dp" >
    <EditText
        android:id="@+id/edt1"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:hint="to" />
    <EditText
        android:id="@+id/edt2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@id/edt1"
        android:hint="subject" />
    <EditText
        android:id="@+id/edt3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:gravity="top"
        android:layout_below="@id/edt1"
        android:layout_toRightOf="@id/edt4"
        android:hint="message" />
    <Button
        android:id="@+id/edt4"
        android:layout_width="100dp"
        android:layout_height="wrap_content"
        android:layout_gravity="right"
        android:layout_below="@id/edt1"
        android:layout_toRightOf="@id/edt2"
        android:text="send" />
</RelativeLayout>

常用属性:

android:layout_above 将该控件的底部至于给定ID的控件之上

android:layout_below 将该控件的顶部至于给定ID的控件之下

android:layout_toLeftOf 将该控件的右边缘和给定ID的控件的左边缘对齐

android:layout_toRightOf 将该控件的左边缘和给定ID的控件的右边缘对齐

android:layout_alignBaseline 该控件的baseline和给定ID的控件的baseline对齐

android:layout_alignBottom 将该控件的底部边缘与给定ID控件的底部边缘

android:layout_alignLeft 将该控件的左边缘与给定ID控件的左边缘对齐

android:layout_alignRight 将该控件的右边缘与给定ID控件的右边缘对齐

android:layout_alignTop 将给定控件的顶部边缘与给定ID控件的顶部对齐

android:alignParentBottom 如果该值为true,则将该控件的底部和父控件的底部对齐

android:layout_alignParentLeft 如果该值为true,则将该控件的左边与父控件的左边对齐

android:layout_alignParentRight 如果该值为true,则将该控件的右边与父控件的右边对齐

android:layout_alignParentTop 如果该值为true,则将空间的顶部与父控件的顶部对齐

android:layout_centerHorizontal 如果值为真,该控件将被至于水平方向的中央

android:layout_centerInParent 如果值为真,该控件将被至于父控件水平方向和垂直方向的中央

android:layout_centerVertical 如果值为真,该控件将被至于垂直方向的中央

 

(三) AbsoluteLayout(不推荐使用)

   绝对布局 .. 就是给定x y坐标,固定一个控件的位置

     常用属性:

     android:layout_x :设置x

     android:layout_y :设置y

     使用绝对布局可以再Graphical Layout中直接拖控件 预览布局的效果

 

(四) 框布局

FrameLayout 感觉类似于 PS图层显示或堆栈

图层的上方会覆盖掉下方的某些区域,除非上方控件透明,

因此在框布局XML文件中定义的控件, 最下方的控件显示在最顶层

    常用属性:

     android:foreground : 前置图片

     android:foregroundGravity : 前置图片重心

     android:measureAllChildren : 在切换显示时是否显示测量所有子组件的大小

     android:layout_gravity: 添加组件的重心

 

(五) 表格布局

<TableRow>表示一行

<span style="font-family:Comic Sans MS;font-size:14px;"><?xml version="1.0" encoding="utf-8"?>

    <TableLayout
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        >
    <TableRow>
        <Button 
            android:text="1_1"
        />
        <Button 
            android:text="1_2"
        />
        <Button 
            android:text="1_3"
        />
    </TableRow>
    <TableRow>
        <Button 
            android:text="2_1"
            android:layout_span="2"
        />
        <Button 
            android:layout_column="2"
            android:text="2_2"
        />
    </TableRow>
             
   </TableLayout>
    
</span>

 

常用属性:

android:stretchColumns    设置可伸展的列。该列可以向行方向伸展,最多可占据一整行。

android:shrinkColumns     设置可收缩的列。当该列子控件的内容太多,已经挤满所在行,那么该子控件的内容将往列方向显示。

android:collapseColumns 设置要隐藏的列。

 

示例:
android:stretchColumns="0"           第0列可伸展

android:shrinkColumns="1,2"         第1,2列皆可收缩

android:collapseColumns="*"         隐藏所有行
说明:列可以同时具备stretchColumns及shrinkColumns属性,若此,那么当该列的内容N多时,将“多行”显示其内容。(这里不是真正的多行,而是系统根据需要自动调节该行的layout_height)

2、单元格属性,有以下2个参数:
android:layout_column    指定该单元格在第几列显示

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

示例:
android:layout_column="1"    该控件显示在第1列

android:layout_span="2"        该控件占据2列

android:collapseColumns : 隐藏指定某列

posted on 2013-01-02 22:35  @与非  阅读(151)  评论(0编辑  收藏  举报