Android布局笔记

2017-12-27 14:44:59

线性布局

LinearLayout
  • LinearLayout是线性布局控件,它包含的子控件将以横向或竖向的方式排列
LinearLayout常用的属性
  • LinearLayout本身常用到得俩个属性
    android: orientation = "vertical" 该属性决定他子类控件的排布方式(vertical:垂直,horizontal:水平)
    android: gravity = "center" 该属性决定他子类的xy的位置
  • 常用到的几个属性值:

    1. center_vertical:垂直(Y轴)居中
    2. center_horizontal:水平(X轴)居中
    3. center:水平垂直都居中
    4. right 子类控件位于当前布局的右边
    5. left子类控件位于当前布局的左边
    6. bottom 子类控件位于当前布局的下面
子类控件在LinearLayout中常用到的属性
  • android: layout_gravity = "bottom" 指本身在当前父容器的XY的一个位置
  • android: layout_weight = "1" 指本身控件占当前父容器的一个比例

相对布局

RelativeLayout
  • RelativeLayout是相对布局控件,它包含的子控件将以控件之间的相对位置或者子类控件相对父类容器的位置的方式排列。
子类控件在RelativeLayout中常用到的属性(相对父容器的一个位置)
  • android: layout_alignParentLeft = "true" 子类控件相对当前父类容器靠左边
  • android: layout_alignParentTop = "true" 子类控件相对父类容器靠上边
  • android: layout_marginLeft = "41dp" 子类控件距离父类容器左边的距离
  • android: layout_marginTop = "33dp" 子类控件距离父类容器上边的距离
  • android: layout_centerInParent = "true" 子类控件相对父类容器即水平居中又垂直居中
  • android: layout_centerHorizontal = "true" 子类控件相对父类容器水平居中
  • android: layout_centerVertical = "true" 子类控件相对父类容器垂直居中
子类控件相对子类控件的一个位置
  • android: layout_below="@+id/button1" 该控件位于给定id控件的底部
  • android: layout_toRightOf="@+id/button1" 该控件位于给定id控件的右边
  • android: layout_above="@+id/button1" 该控件位于给定id控件的上面
  • android: layout_toLeftOf="@+id/button1" 该控件位于给定控件的左边
  • android: layout_alignBaseline="@+id/button1" 该控件的内容与给定id控件的内容在一条线上
  • android: layout_alignBottom 该控件的底部边缘与给定ID控件的底部边缘对齐
  • android: layout_alignLeft 该控件的左边缘与给定ID控件的左边缘对齐
  • android: layout_alignRight 该控件的右边缘与给定ID控件的右边缘对齐
  • android: layout_alignTop 该控件的顶部边缘与给定ID控件的顶部对齐

帧布局(FrameLayout)

FrameLayout
  • 在这个布局中,所有的子元素都不能被指定放置的位置,它们统统放于这块区域的左上角,并且后面的了元素直接覆盖在前面的子元素之上,将前面的子元素部分和全部遮挡。

下面是两个布局文件案例

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <TextView
        android:layout_gravity="center"
        android:id="@+id/textView2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:background="#785435"
        android:width="200dp"
        android:height="200dp"
        android:text="第一个页面" />

    <TextView
        android:layout_gravity="center"
        android:id="@+id/textView3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:width="150dp"
        android:height="150dp"
        android:background="#985764"
        android:text="第二个页面" />

    <TextView
        android:layout_gravity="center"
        android:id="@+id/textView4"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:width="100dp"
        android:height="100dp"
        android:background="#564238"
        android:text="第三个页面" />

    <TextView
        android:layout_gravity="center"
        android:id="@+id/textView5"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:width="80dp"
        android:height="80dp"
        android:background="#185435"
        android:text="第四个页面" />
</FrameLayout>

效果图

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent" android:layout_height="match_parent">

    <ProgressBar
        android:layout_gravity="center"
        android:id="@+id/progressBar"
        style="?android:attr/progressBarStyle"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />

    <TextView
        android:layout_gravity="center"
        android:id="@+id/textView6"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="10%" />
</FrameLayout>

效果图

绝对布局(AbsoluteLayout)

AbsoluteLayout

AbsoluteLayout(绝对布局),又可以叫做坐标布局,可以直接指定子元素的绝对位置(xy)
由于手机屏幕差别比较大,使用绝对定位的适应性会比较差,在屏幕的适配上有缺陷

AbsoluteLayout子类控件的属性
  • android: layout_x = "35dip" 控制当前子类控件的x位置
  • android: layout_y = "40dip" 控件当前子类控件的y位置

表格布局(TableLayout)

TableLayout
  • TableLayout表格布局模型以行列的形式管理子控件,每一行为一个TableRow的对象,当然也可以是一个View对象。
TableLayout的属性(全局属性)
  • android: collapseColumns = "1,2" 隐藏从0开始的索引列。列直接必须用逗号隔开:1,2,5
  • android: shrinkColumns = "1,2" 收缩从0开始的索引列。当可收缩的列太宽(内容过多)不会被挤出屏幕,列直接必须用逗号隔开:1,2,5你可以通过"*"代替收缩所有列,注意一列能同时表示收缩和拉伸。
  • android: stretchColumns = "1,2" 拉伸从0开始的索引列。以填满剩下的多余空白空间,列直接必须用逗号隔开:1,2,5,你可以通过"*"你代替收缩所有列。注意一列能同时表示收缩的拉伸。
  • android: layout_column="1" 该控件显示在第2列
  • android: layout_span="2" 该控件占据2列