Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲

Android 布局學習:Layout 詳解(一)

編輯:關於 Android 程式設計

聲明 layout 有兩種方式:

1. 在 XML 文件中聲明 UI 組件。這是推薦做法,結構清晰、易於預覽,也方便與設計稿對照。
2. 在運行時通過代碼實例化布局元素。以編碼方式創建 View 或 ViewGroup 對象,並操作它們的屬性。

兩者並非互斥。實戰中常見的組合是:主體布局寫在 XML 裡,運行時再動態往容器中 addView() 一些列表項、表單行。所以兩種方式都要掌握。

一、用代碼構建布局

import android.app.Activity;
import android.graphics.Color;
import android.os.Bundle;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.TextView;

public class MainActivity extends Activity {

    private LinearLayout linearLayout;
    private TextView textView;
    private Button button;

    public static final int VERTICAL = 1;
    public static final int MATCH_PARENT = -1;
    public static final int WRAP_CONTENT = -2;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        // 以編碼方式添加 layout
        linearLayout = new LinearLayout(this);
        // 設置 LinearLayout 方向:0 為水平,1 為垂直,默認水平
        linearLayout.setOrientation(VERTICAL);
        // 設置布局參數,-1 為 MATCH_PARENT,-2 為 WRAP_CONTENT
        linearLayout.setLayoutParams(
                new ViewGroup.LayoutParams(MATCH_PARENT, MATCH_PARENT));

        textView = new TextView(this);
        textView.setText("ThisIsATextView");
        textView.setBackgroundColor(Color.RED);
        textView.setLayoutParams(
                new ViewGroup.LayoutParams(MATCH_PARENT, WRAP_CONTENT));

        button = new Button(this);
        button.setText("ThisIsAButton");
        button.setBackgroundColor(Color.GREEN);
        button.setLayoutParams(
                new ViewGroup.LayoutParams(MATCH_PARENT, WRAP_CONTENT));

        linearLayout.addView(button);
        linearLayout.addView(textView);

        // 布局寫好後,別忘了添加到 Activity 中
        setContentView(linearLayout);
    }
}

這裡有兩個新手常犯的錯誤值得強調:

其一,忘了 setContentView。 光把 View 對象 new 出來並組裝好是不夠的,必須交給 Activity 才會顯示。

其二,LayoutParams 用錯類型。 子 View 的 LayoutParams 必須與其父容器的 LayoutParams 類型匹配,否則會拋 ClassCastException。例如 LinearLayout 的子元素要用 LinearLayout.LayoutParams,才能設置 layout_weight。

二、每個布局文件的根元素

每個 layout 文件必須包含一個確定的根元素,它必須是 View 或 ViewGroup 的對象。那這兩個類各自負責什麼?

View
為用戶界面組件提供基本的搭建區域。View 類是所有可視控件的老祖宗,widget 包下的 Button、TextView、ImageView 等都用來創建可交互的 UI 組件。同時 View 也是 ViewGroup 的父類。

ViewGroup
是各類 Layout 的父類,本質上是「不可見的容器」——它自己不顯示內容,負責容納其他 View 或 ViewGroup,並定義它們的擺放規則(布局屬性)。LinearLayout、RelativeLayout、FrameLayout、ConstraintLayout 都是它的子類。

構建界面層的思路,就是通過嵌套容器與添加子元素,一層層把視圖樹「長」出來。

三、對應的 XML 寫法

<!-- 根元素 LinearLayout 是 ViewGroup 的子類 -->
<LinearLayout xmlns:android="https://schemas.android.com/apk/res/android"
    xmlns:tools="https://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical" >

    <!-- 添加子組件來豐富視圖層 -->
    <Button
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="#f00"
        android:layout_weight="1"
        android:text="ThisIsAButton" />

    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="#0f0"
        android:text="ThisIsATextView" />

</LinearLayout>

四、幾個必備屬性

layout_width / layout_height
每個 View 都必須指定,取值有三種:match_parent(與父容器等寬/高)、wrap_content(包裹自身內容)、具體數值(如 48dp)。

orientation
LinearLayout 專屬,決定子元素排列方向。

layout_weight
LinearLayout 專屬,按權重比例分配剩餘空間。注意:權重是在測量完 wrap_content 部分之後,對剩餘空間做分配,所以搭配 layout_height="0dp" 使用性能更好,能避免多餘的二次測量。

padding 與 margin
padding 是容器內部的留白,算在自身尺寸裡;margin 是與兄弟節點的外間距,不算在自身尺寸裡。兩者含義不同,不要混用。

id
寫成 @+id/xxx 表示新建 id 資源,@id/xxx 表示引用已有的。代碼中通過 findViewById() 獲取。

五、一點性能提醒

視圖層嵌套越深,測量和布局的開銷越大(measure 與 layout 是遞歸的)。實踐中建議:

  • 能用 ConstraintLayout 一層搞定的,不要用三四層 LinearLayout 嵌套。
  • 用 <merge> 標簽消除引入布局時多餘的根節點。
  • 用 <include> 複用片段,用 <ViewStub> 延遲加載不常用的複雜視圖。
  • 複雜列表項不要用層層嵌套的布局,改用自定義 View 直接繪製往往更快。

下一篇再談談各類 Layout 的具體特性與選型。

  1. 上一篇文章:
  2. 下一篇文章:
Copyright © Android教程網 All Rights Reserved