天天看點

【Android -- UI 開發】六大布局

網上有人比喻的很好:布局好比是建築裡的架構,元件按照布局的要求依次排列,就組成了用于看見的漂亮界面了。

一、布局類型

【Android -- UI 開發】六大布局

1. LinearLayout (線性布局)

LinearLayout 是最常用的布局方式,在 XML 檔案中使用 标記。它會将容器裡的 UI 元件一個一個挨着排列起來。但是 LinearLayout 不會換行,當 UI 元件超出螢幕之後,則不會被顯示出來。 LinearLayout 有兩個重要的 XML 屬性:android:gravity(對齊方式);android:orientation(排列方式)。

android:orientation(排列方式),設定了 LinearLayout 中包含的 UI 元件的排列方式,有兩個選項vertical(豎向)、horizontal(橫向,預設值)

android:gravity(對齊方式),設定 LinearLayout 中包含 UI 元件的對齊方式,其選項很多,常用上(top)、下(bottom)、左(left)、右(right)。

  • 示例代碼:
<?xml version="1.0" encoding="utf-8"?>
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal" >
        <Button
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="4" >
        </Button>
        <Button
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="5" >
        </Button>
        <Button
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="6" >
        </Button>
        <Button
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="+" >
        </Button>
    </LinearLayout>      

2. RelativeLayout (相對布局)

RelativeLayout,其内子元件的位置總是相對兄弟UI元件、父親容器來決定的。比如UI元件A相對于UI元件B的位置進行定位,那麼UI元件B需要在UI元件A之前定義。

相對布局用到的主要屬性:

  • android:layout_below:在某元素的下方。
  • android:layout_above:在某元素的上方。
  • android:layout_toLeftOf:在某元素的左邊。
  • android:layout_toRightOf:在某元素的右邊。
  • android:layout_alignXxx:控制與某元素的邊界對其方式。
  • 示例代碼:
<?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" >
    <Button android:id="@+id/btn1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true"
        android:layout_centerHorizontal="true"
        android:text="Button1"
        ></Button>
    <Button android:id="@+id/btn2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_toLeftOf="@id/btn1"
        android:layout_above="@id/btn1"
        android:text="Button2"
        ></Button>
    <Button android:id="@+id/btn3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_toRightOf="@id/btn1"
        android:layout_above="@id/btn1"
        android:text="Button3"
        ></Button>
    <Button android:id="@+id/btn4"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_toRightOf="@id/btn2"
        android:layout_toLeftOf="@id/btn3"
        android:layout_above="@id/btn2"
        android:text="Button4"
        ></Button>
</RelativeLayout>      

3. TableLayout (表格布局)

表格布局,采用行、列的形式來管理UI元件,TableLayout 通過 TableRow、其他UI元件來控制表格的行數和列數。

每次向 TableLayout 中添加一個 TableRow,該 TableRow 就是一個表格行,TableRow 也是容器,是以它也可以不斷添加其他元件,沒添加一個子元件,該表格就增加一列。如果直接向 TableLayout 中添加元件,那麼這個元件将直接占用一行。

TableLayout支援的XML屬性:

  • android:collapseColumns:設定需要被隐藏的列序号。
  • android:shrinkColumns:設定需要被收縮的列序号。
  • android:stretchColumns:設定需要被拉伸的列序号。

    注意:TableLayout中所謂的序列号是從0開始計算的。

示例代碼(電腦):

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:padding="12dp"
    android:background="@drawable/shape_layout_bg"
    android:orientation="vertical">

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content">
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="電腦"
            android:textSize="20sp"
            android:textColor="@color/white"
            android:layout_centerInParent="true"/>

        <ImageView
            android:id="@+id/iv_exit"
            android:layout_width="35dp"
            android:layout_height="35dp"
            android:src="@mipmap/ic_main_exit"
            android:layout_alignParentRight="true"
            android:layout_centerVertical="true"/>
    </RelativeLayout>

    <EditText
        android:id="@+id/result_text"
        android:layout_width="match_parent"
        android:layout_height="80dp"
        android:textSize="30sp"
        android:padding="20dp"
        android:layout_marginTop="10dp"
        android:layout_gravity="end"
        android:editable="false"
        android:textAlignment="textEnd"
        android:singleLine="true"
        android:background="@drawable/shape_edt_bg"
        android:text="0"/>

    <TableLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="10dp">
        <TableRow
            android:layout_width="match_parent"
            android:layout_height="wrap_content">
            <Button
                android:id="@+id/ac_btn"
                android:layout_width="0dp"
                android:layout_weight="1"
                android:layout_height="50dp"
                android:textSize="24sp"
                android:layout_margin="3dp"
                android:textColor="@color/white"
                android:background="@drawable/shape_btn_bg"
                android:text="AC"/>

            <Button
                android:id="@+id/delete_btn"
                android:layout_width="0dp"
                android:layout_weight="1"
                android:layout_height="50dp"
                android:textSize="24sp"
                android:textColor="@color/white"
                android:layout_margin="3dp"
                android:background="@drawable/shape_btn_bg"
                android:text="Del"/>

            <Button
                android:id="@+id/percent_btn"
                android:layout_width="0dp"
                android:layout_weight="1"
                android:layout_height="50dp"
                android:textSize="24sp"
                android:textColor="@color/white"
                android:layout_margin="3dp"
                android:background="@drawable/shape_btn_bg"
                android:text="%"/>

            <Button
                android:id="@+id/divide_btn"
                android:layout_width="0dp"
                android:layout_weight="1"
                android:layout_height="50dp"
                android:textSize="24sp"
                android:textColor="@color/white"
                android:layout_margin="3dp"
                android:background="@drawable/shape_btn_bg"
                android:text="÷"/>

        </TableRow>

        <TableRow
            android:layout_width="match_parent"
            android:layout_height="wrap_content">

            <Button
                android:layout_width="0dp"
                android:layout_weight="1"
                android:layout_height="50dp"
                android:textSize="24sp"
                android:textColor="@color/white"
                android:layout_margin="3dp"
                android:background="@drawable/shape_btn_bg"
                android:text="7"
                android:id="@+id/num_seven" />

            <Button
                android:layout_width="0dp"
                android:layout_weight="1"
                android:layout_height="50dp"
                android:textSize="24sp"
                android:textColor="@color/white"
                android:layout_margin="3dp"
                android:background="@drawable/shape_btn_bg"
                android:text="8"
                android:id="@+id/num_eight" />

            <Button
                android:layout_width="0dp"
                android:layout_weight="1"
                android:layout_height="50dp"
                android:textSize="24sp"
                android:textColor="@color/white"
                android:layout_margin="3dp"
                android:background="@drawable/shape_btn_bg"
                android:text="9"
                android:id="@+id/num_nine" />

            <Button
                android:layout_width="0dp"
                android:layout_weight="1"
                android:layout_height="50dp"
                android:textSize="24sp"
                android:textColor="@color/white"
                android:layout_margin="3dp"
                android:background="@drawable/shape_btn_bg"
                android:text="×"
                android:id="@+id/multiply_btn" />
        </TableRow>

        <TableRow
            android:layout_width="match_parent"
            android:layout_height="wrap_content">

            <Button
                android:layout_width="0dp"
                android:layout_weight="1"
                android:layout_height="50dp"
                android:textSize="24sp"
                android:textColor="@color/white"
                android:layout_margin="3dp"
                android:background="@drawable/shape_btn_bg"
                android:text="4"
                android:id="@+id/num_four" />

            <Button
                android:layout_width="0dp"
                android:layout_weight="1"
                android:layout_height="50dp"
                android:textSize="24sp"
                android:textColor="@color/white"
                android:layout_margin="3dp"
                android:background="@drawable/shape_btn_bg"
                android:text="5"
                android:id="@+id/num_five" />

            <Button
                android:layout_width="0dp"
                android:layout_weight="1"
                android:layout_height="50dp"
                android:textSize="24sp"
                android:textColor="@color/white"
                android:layout_margin="3dp"
                android:background="@drawable/shape_btn_bg"
                android:text="6"
                android:id="@+id/num_six" />

            <Button
                android:layout_width="0dp"
                android:layout_weight="1"
                android:layout_height="50dp"
                android:textSize="24sp"
                android:textColor="@color/white"
                android:layout_margin="3dp"
                android:background="@drawable/shape_btn_bg"
                android:text="-"
                android:id="@+id/subtract_btn" />
        </TableRow>

        <TableRow
            android:layout_width="match_parent"
            android:layout_height="wrap_content">

            <Button
                android:layout_width="0dp"
                android:layout_weight="1"
                android:layout_height="50dp"
                android:textSize="24sp"
                android:textColor="@color/white"
                android:layout_margin="3dp"
                android:background="@drawable/shape_btn_bg"
                android:text="1"
                android:id="@+id/num_one" />

            <Button
                android:layout_width="0dp"
                android:layout_weight="1"
                android:layout_height="50dp"
                android:textSize="24sp"
                android:textColor="@color/white"
                android:layout_margin="3dp"
                android:background="@drawable/shape_btn_bg"
                android:text="2"
                android:id="@+id/num_two" />

            <Button
                android:layout_width="0dp"
                android:layout_weight="1"
                android:layout_height="50dp"
                android:textSize="24sp"
                android:textColor="@color/white"
                android:layout_margin="3dp"
                android:background="@drawable/shape_btn_bg"
                android:text="3"
                android:id="@+id/num_three" />

            <Button
                android:layout_width="0dp"
                android:layout_weight="1"
                android:layout_height="50dp"
                android:textSize="24sp"
                android:textColor="@color/white"
                android:layout_margin="3dp"
                android:background="@drawable/shape_btn_bg"
                android:text="+"
                android:id="@+id/plus_btn" />
        </TableRow>

        <TableRow
            android:layout_width="match_parent"
            android:layout_height="wrap_content">

            <Button
                android:id="@+id/num_zero"
                android:layout_width="0dp"
                android:layout_weight="2"
                android:layout_height="50dp"
                android:textSize="24sp"
                android:textColor="@color/white"
                android:layout_margin="3dp"
                android:background="@drawable/shape_btn_bg"
                android:text="0"/>

            <Button
                android:id="@+id/dot_btn"
                android:layout_width="0dp"
                android:layout_weight="1"
                android:layout_height="50dp"
                android:textSize="24sp"
                android:textColor="@color/white"
                android:layout_margin="3dp"
                android:background="@drawable/shape_btn_bg"
                android:text="."/>

            <Button
                android:id="@+id/equal_btn"
                android:layout_width="0dp"
                android:layout_weight="1"
                android:layout_height="50dp"
                android:textSize="24sp"
                android:textColor="@color/white"
                android:layout_margin="3dp"
                android:background="@drawable/shape_btn_equal"
                android:text="="/>
        </TableRow>

    </TableLayout>


</LinearLayout>      

4. FrameLayout (架構布局)

  • 放入的所有控件都被放置在最左上的區域;
  • 無法為控件指定一個确切的位置;
  • 下一個子控件會重疊覆寫上一個子控件。

5. AbsoluteLayout (絕對布局)

  • 采用坐标軸的方式定位控件;
  • 左上角是(0,0)點,往右X軸遞增,往下Y軸遞增;
  • 定位屬性: android:layout_x、android:layout_y

6. ConstraintLayout (限制布局)

  • ConstraintLayout是Android Studio 2.2中主要的新增功能之一;
  • ConstraintLayout非常适合使用可視化的方式來編寫界面;
  • ConstraintLayout 可以有效地解決布局嵌套過多的問題。

    目前,Android Studio 是 2.2或以上版本預設布局檔案首選就是 ConstraintLayout。

基本使用請參考:​​Android新特性介紹,ConstraintLayout完全解析​​

7.選擇器(Selector)

7.1 作用

通過設定選擇器(selector)可使控件 在不同操作下(預設、點選等) 顯示不同樣式

7.2 屬性

名稱 說明
android:drawable 放一個drawable資源
android:state_pressed 按下狀态,如一個按鈕觸摸或者點選。
android:state_focused 取得焦點狀态,比如使用者選擇了一個文本框。
android:state_hovered 光标懸停狀态,通常與focused state相同,它是4.0的新特性
android:state_selected 選中狀态
android:state_enabled 能夠接受觸摸或者點選事件
android:state_checked 被checked了,如:一個RadioButton可以被check了。
<?xml version="1.0" encoding="UTF-8"?>
< selector xmlns:android="http://schemas.android.com/apk/res/android">

 < !-- 指定按鈕按下時的圖檔 -->
 <item android:state_pressed="true"  
       android:drawable="@drawable/start_down"
 />
 < !-- 指定按鈕松開時的圖檔 --> 
 <item android:state_pressed="false"
       android:drawable="@drawable/start"
 />
< /selector>      
<Button
  android:id="@+id/startButton"
  android:layout_width="wrap_content"
  android:layout_height="wrap_content"
  android:background="@drawable/button_selector" 
/>