iT邦幫忙

2025 iThome 鐵人賽

DAY 5
0

昨天的文章有提到LinearLayout,LinearLayout是 Android 中一種常用的佈局容器,能夠讓其子元件依照垂直(vertical)或水平(horizontal)方向來排列。


基本語法及常用屬性說明

linearlayout會將元件包在裡面,以下是基礎語法

屬性 說明
android:orientation 排列方向(vertical 垂直、horizontal 水平)
android:layout_width/height 寬與高(match_parent, wrap_content, 或數值如 100dp)
android:layout_weight 權重,用於分配空間(常與 0dp 結合使用)
android:gravity 對齊方式(作用於所有子項)
android:layout_gravity 單個子項在父容器中的對齊方式
android:padding/margin 元件內邊距/外邊距

實際應用:

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

    <!-- 子元件放這裡 -->
    
</LinearLayout>

範例一:垂直排版兩個按鈕

如果是希望兩個按鈕垂直排列,我們需要在LinearLayout中將orientation設置成vertical,並在中間插入兩個button,

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical"
    android:padding="16dp">

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

    <Button
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Button 2" />
</LinearLayout>

範例二:水平排列並平均分配寬度(使用 layout_weight)

水平排列跟垂直排列差別不大,不同的是在LinearLayout中將orientation設置成horizontal而非vertical,這樣即可改變方向。

平均分配寬度就要用到文章開頭提到的layout_weight,他是用於分配空間的,當你想將寬度用比例來分配的話就須將layout_width改為"0dp",接下來就可以在button加上layout_weight,舉例來說layout_weight="1"就是比例為1,兩個button都加上則會使寬度被平均分為1:1。

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal">

    <Button
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:text="Left" />

    <Button
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:text="Right" />
</LinearLayout>

LinearLayout可以反覆套娃,你可以直接在一個水平的LinearLayout中塞多個垂直的LinearLayout來形成下圖,但若套了太多層也會影響到程式碼的可讀性,這點須多加注意
image

範例程式碼:

 <!-- 第二區塊:左圖垂直、右圖垂直 -->
<LinearLayout
    android:layout_width="300dp"
    android:layout_height="wrap_content"
    android:layout_marginTop="5dp"
    android:orientation="horizontal">

    <!-- 左邊:兩張垂直圖 -->
    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginStart="15dp"
        android:layout_marginEnd="15dp"
        android:orientation="vertical">

        <ImageView
            android:id="@+id/main_activityquery_iv"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginBottom="18dp"
            app:srcCompat="@drawable/searchactivity" />

        <ImageView
            android:id="@+id/main_shopquery_iv"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            app:srcCompat="@drawable/searchshop" />

    </LinearLayout>

    <!-- 右邊:兩張垂直按鈕 -->
    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginEnd="15dp"
        android:orientation="vertical">

        <ImageButton
            android:id="@+id/main_point_ibtn"
            android:layout_width="120dp"
            android:layout_height="164dp"
            android:layout_marginTop="0dp"
            android:layout_marginBottom="10dp"
            android:contentDescription="開啟紅利點數選單"
            app:srcCompat="@drawable/point" />

        <ImageButton
            android:id="@+id/main_setting_ibtn"
            android:layout_width="121dp"
            android:layout_height="68dp"
            android:layout_marginBottom="0dp"
            android:contentDescription="開啟設定"
            app:srcCompat="@drawable/setting" />

    </LinearLayout>
</LinearLayout>

PS. LinearLayout 適合用於簡單且排列方向單一的介面。若有複雜對齊或重疊需求,建議使用 ConstraintLayout。


上一篇
Day 04. Android Studio使用(下)
下一篇
Day 06.ImageView
系列文
Android 新手的 30 天進化論:從初學者到小專案開發者7
圖片
  熱門推薦
圖片
{{ item.channelVendor }} | {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

尚未有邦友留言

立即登入留言