Android线性布局与表格布局

UI组件:布局管理器

以ViewGroup为基类派生的布局管理器

为了更好的管理Android应用的用户界面里的各组件,Android提供了布局管理器。通过布局管理器,Android应用的图形用户界面具有良好的平台无关性。
为了让这个组件在不同手机屏幕上都能运行良好–不同手机屏幕的分辨率、尺寸并不完全相同,如果让程序手动控制每个组件的大小,位置,则将给编程带来巨大的困难。为了解决这个问题,Android提供了布局管理器,布局管理器可以根据运行平台来调整组件的大小,我们要做的,是为容器选择合适的布局管理器。
与Swing界面编程不同的是,Android的布局管理器本身就是一个UI组件,所以的布局管理器都是ViewGroup的子类。下面显示了Android布局管理器的类图。
Android线性布局与表格布局_第1张图片
从图片可以看出,所有的布局都可以作为容器类使用,因此可以调用多个重载的addView()向布局管理器中添加组。实际上,我们完全可以用一个布局管理器嵌套到其他布局管理器中–因为布局管理器也继承了View,也可以作为普通的UI组件使用。

线性布局

线性布局由LinearLayout类来代表,它们会将容器里的组件一个一个的排列起来。 LinearLayout可以控制各组件横向排列,也可以控制组件纵向排列。
Android 的线性布局不会换行,当组件一个一个排列到头时,剩下的组件不会被显示出来

垂直线性布局



  
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
    android:layout_width="match_parent"  
    android:layout_height="match_parent"  
    android:orientation="vertical" >  

    <EditText   
        android:layout_width="100dp"  
        android:layout_height="wrap_content"  
        android:text="按钮1"/>
        />  

    <Button   
        android:layout_width="100dp"  
        android:layout_height="wrap_content"  
        android:text="按钮2"/>  

    <Button   
        android:layout_width="100dp"  
        android:layout_height="wrap_content"  
        android:text="按钮3"/>  

LinearLayout>  

Android线性布局与表格布局_第2张图片

水平线性布局


<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="horizontal" >

<Button 
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="按钮1"
    />

<Button 
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="按钮2"
    />
<Button 
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="按钮3"
    />
<Button 
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="按钮4"
    />
LinearLayout>

这里写图片描述

表格布局

表格布局由TableLayout代表,TableLayout继承了LinearLayout,因此他的本质跟线性布局相同
在表格布局管理器中,可以为单元格设置3种行为方式

Shrinkable:如果某个列被设为 Shrinkable,那么该列的所有单元格的宽度可以被收缩
Stretchable:如果某个列被设为Stretchable,那么该列的所有单元格可以被拉伸
Collapsed:如果某个列被设为Collapsed,那么该列的所有单元格会被隐藏

表格布局


<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
     >
    <TableRow >
        <Button
            android:id="@+id/button1"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="Button1" />
    TableRow>
    <TableRow >
        <Button
            android:id="@+id/button2"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="Button2" />
    TableRow>
    <TableRow >
        <Button
            android:id="@+id/button3"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="Button3" />
    TableRow>
    <TableRow >
        <Button
            android:id="@+id/button4"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="Button4" />
    TableRow>
    <View
        android:layout_height="2dip"
        android:background="#FF909090" />
    <TableRow >
        <Button
            android:id="@+id/button5"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="Button5" />
        <Button
            android:id="@+id/button6"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="Button6" />
        <Button
            android:id="@+id/button7"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="Button7" />
        <Button
            android:id="@+id/button8"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="Button8" />
    TableRow>
TableLayout>

Android线性布局与表格布局_第3张图片

你可能感兴趣的:(Android线性布局与表格布局)