天天看点

Android学习笔记之布局

Android SDK支持了五种布局。分别是FrameLayout(框架布局),LinearLayout(线性布局),RelativeLayout(相对布局),TableLayout(表格布局)和AbsoluteLayout(绝对布局)。不过绝对布局在Android的2.0中被标记为已过期,

框架布局(FrameLayout)

所有添加到这个布局的视图都以层叠的方式显示。第一个添加到框架布局中的视图显示在最底层,上一层会覆盖下一层的视图

框架布局在XML布局文件中使用< FrameLayout>标签进行配置。

<FrameLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">

    <TextView 
        android:id="@+id/textView" 
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />

    <Button
        android:id="@+id/button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"/>
</FrameLayout>
           

android:layout_width:当前视图的宽度,可以是固定的值也可以是3个枚举类型: wrap_content ,fill_parent和match_parent。其中wrap_content表示框架的宽度会随着视图中内容的大小自动调整。而fill_parent和match_parent的含义完全一样。都表示当前视图尽可能充满父视图的水平视图 但仍然会受到android:layout_weight属性的影响

android:layout_height:当前视图的高度,属性值同上,android:layout_height和android:layout_width是必选的。

abdroid:layout_gravity:当前视图在父视图中的位置。

android:layout_margin:表示当前视图的外距,可以单独对一边或多边设置,也可以一起设置。

线性布局(LinearLayout)

线性布局分为水平线性布局和垂直线性布局。通过android:orientation属性可以设置线性布局方向,属性值是horizonal和vertical。默认死horizontal,方向水平。

< LinearLayout>标签有个非常重要的gravity属性,用于控制布局中视图的位置。

<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:gravity="left"
    android:orientation="vertical">
    <Button
        android:text="按钮1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"/>
    <Button
        android:text="按钮2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"/>
    <Button
        android:text="按钮3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"/>
</LinearLayout>
           

使用以上代码将会出现下面的效果(左图) 如果将gravity属性值该位center将出现(右图)

Android学习笔记之布局
Android学习笔记之布局

< LinearLayout>标签中的子标签还可以使用layout_gravity和layout_weight属性来设置每一个位图的位置。

layout_gravity属性的可取值与gravity属性的可取值相同,表示当前视图在布局中的位置。layout_weight属性需要设置一个非负整数值,如果该值大于0,线性布局会根据水平或垂直方向以及不同layout_weight属性值占所有视图的layout_weight 属性值之和的比例为这些视图分配自己所占用的区域,视图将按相应比例拉伸。layout_weight属性值越小,占的比重越大。

相对布局(RelativeLayout)

相对布局可以设置某一个视图相对于其他视图的位置,包括上,下,左和右。属性分别是android:layout_above,android:layout_below,android:layout_toLeftOf,android:layout_toRightOf。还可以通过android:layout_alignBaseline属性设置视图的低端对齐。

这五个属性的值必须是已经存在的资源ID,例子。

<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:gravity="center"
    android:orientation="vertical">
    <Button
        android:id="@+id/btn1"
        android:text="按钮1"
        android:layout_gravity="center_horizontal"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"/>
    <Button
        android:id="@+id/btn2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="按钮2"
        android:layout_gravity="right"
        android:layout_toRightOf="@id/btn1"
        android:layout_below="@id/btn1"/>
    <Button
        android:id="@+id/btn3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="按钮3"
        android:layout_toLeftOf="@id/btn2"
        android:layout_below="@id/btn2"/>
    <Button
        android:id="@+id/btn4"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="按钮4"
        android:layout_toRightOf="@id/btn2"
        android:layout_above="@id/btn2"/>
    <Button
        android:id="@+id/btn5"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="按钮5"
        android:layout_toRightOf="@id/btn2"
        android:layout_below="@id/btn2"/>
</RelativeLayout>
           

会出现。

Android学习笔记之布局

表格布局(TableLayout)

表格布局可将视图按行,列进行排列。一个表格布局有一个< TableLayout>标签和若干个< TableRow>标签组成。

表格布局在实现行列效果中并不常用,一般会使用GridView控件代替。
           

绝对布局(AbsoluteLayout)

通过绝对布局,可以任意设置视图的位置,android:layout_x和android:layout_y属性进行设置

继续阅读