Android中LinearLayout線性布局使用詳解

Android中LinearLayout線性布局使用詳解

LinearLayout(線性布局)是Android中最基礎、最常用的布局之一,它按照水平或垂直方向依次排列子視圖。

基本特性

  1. 方向性:可以設置為水平(horizontal)或垂直(vertical)排列
  2. 權重:支持通過weight屬性分配剩余空間
  3. 簡單高效:布局計算簡單,性能較好
  4. 嵌套組合:常與其他布局嵌套使用實現復雜界面

基本屬性

核心屬性

  • android:orientation:布局方向

    • vertical:垂直排列(默認)
    • horizontal:水平排列
  • android:gravity:子視圖在布局內的對齊方式(定義在容器視圖上)

    • top子視圖頂部對齊/bottom子視圖底部對齊/left子視圖左對齊/right子視圖右對齊
    • center_vertical垂直居中/center_horizontal水平居中/center水平居中
    • 可以組合使用,如left|center_vertical
  • android:layout_gravity:單個子視圖在布局內的對齊方式(取值同上,定義在子視圖上)

權重屬性

  • android:layout_weight:子視圖的權重,用于分配剩余空間
    • 值越大,分配的空間越多(android:layout_weight:1類似于css中的flex:1
    • 常與layout_widthlayout_height設為0dp配合使用

基本用法示例

垂直布局示例

<LinearLayoutxmlns:android="http://schemas.android.com/apk/res/android"android:layout_width="match_parent"android:layout_height="match_parent"android:orientation="vertical"android:gravity="center_horizontal"android:padding="16dp"><Buttonandroid:layout_width="wrap_content"android:layout_height="wrap_content"android:text="按鈕1"/><Buttonandroid:layout_width="wrap_content"android:layout_height="wrap_content"android:text="按鈕2"/><Buttonandroid:layout_width="wrap_content"android:layout_height="wrap_content"android:text="按鈕3"/>
</LinearLayout>

水平布局示例

<LinearLayoutxmlns:android="http://schemas.android.com/apk/res/android"android:layout_width="match_parent"android:layout_height="wrap_content"android:orientation="horizontal"android:gravity="center_vertical"><ImageViewandroid:layout_width="48dp"android:layout_height="48dp"android:src="@mipmap/ic_launcher"/><TextViewandroid:layout_width="wrap_content"android:layout_height="wrap_content"android:text="這是一個水平排列的文本"android:layout_marginStart="8dp"/>
</LinearLayout>

權重(weight)的高級用法

權重是LinearLayout最強大的特性之一,可以實現比例分配空間。

等分空間

<LinearLayoutandroid:layout_width="match_parent"android:layout_height="wrap_content"android:orientation="horizontal"><Buttonandroid:layout_width="0dp"android:layout_height="wrap_content"android:layout_weight="1"android:text="按鈕1"/><Buttonandroid:layout_width="0dp"android:layout_height="wrap_content"android:layout_weight="1"android:text="按鈕2"/><Buttonandroid:layout_width="0dp"android:layout_height="wrap_content"android:layout_weight="1"android:text="按鈕3"/>
</LinearLayout>

不等比例分配

<LinearLayoutandroid:layout_width="match_parent"android:layout_height="wrap_content"android:orientation="vertical"><TextViewandroid:layout_width="match_parent"android:layout_height="0dp"android:layout_weight="2"android:text="占2/5空間"android:background="#FF5722"/><TextViewandroid:layout_width="match_parent"android:layout_height="0dp"android:layout_weight="3"android:text="占3/5空間"android:background="#4CAF50"/>
</LinearLayout>

常用技巧

1. 分割線使用

<LinearLayoutandroid:layout_width="match_parent"android:layout_height="wrap_content"android:orientation="vertical"android:showDividers="middle|beginning|end"android:divider="@drawable/divider_line"android:dividerPadding="8dp"><!-- 子視圖 -->
</LinearLayout>

需要定義divider_line的drawable:

<!-- res/drawable/divider_line.xml -->
<shape xmlns:android="http://schemas.android.com/apk/res/android"><size android:width="1dp" android:height="1dp"/><solid android:color="#CCCCCC"/>
</shape>

2. 基線對齊(針對文本)

<LinearLayoutandroid:layout_width="match_parent"android:layout_height="wrap_content"android:orientation="horizontal"android:baselineAligned="true"><!-- 不同大小的文本會按照基線對齊 -->
</LinearLayout>

3. 嵌套使用實現復雜布局

<LinearLayoutandroid:layout_width="match_parent"android:layout_height="match_parent"android:orientation="vertical"><!-- 頂部標題欄 --><LinearLayoutandroid:layout_width="match_parent"android:layout_height="48dp"android:orientation="horizontal"android:background="#3F51B5"><!-- 標題欄內容 --></LinearLayout><!-- 中間內容區 --><LinearLayoutandroid:layout_width="match_parent"android:layout_height="0dp"android:layout_weight="1"android:orientation="horizontal"><!-- 左側導航 --><LinearLayoutandroid:layout_width="120dp"android:layout_height="match_parent"android:orientation="vertical"><!-- 導航內容 --></LinearLayout><!-- 右側內容 --><ScrollViewandroid:layout_width="0dp"android:layout_height="match_parent"android:layout_weight="1"><!-- 可滾動內容 --></ScrollView></LinearLayout><!-- 底部按鈕欄 --><LinearLayoutandroid:layout_width="match_parent"android:layout_height="48dp"android:orientation="horizontal"><!-- 底部按鈕 --></LinearLayout>
</LinearLayout>

代碼中動態修改LinearLayout

LinearLayout linearLayout = findViewById(R.id.my_linear_layout);// 修改方向
linearLayout.setOrientation(LinearLayout.HORIZONTAL);// 添加子視圖
Button newButton = new Button(this);
newButton.setText("動態添加的按鈕");LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT,LinearLayout.LayoutParams.WRAP_CONTENT);
params.weight = 1; // 設置權重linearLayout.addView(newButton, params);

性能優化建議

  1. 減少嵌套:避免多層LinearLayout嵌套,會增加布局計算復雜度
  2. 合理使用weight:weight會觸發兩次測量,影響性能
  3. 考慮使用ConstraintLayout:對于復雜布局,ConstraintLayout通常性能更好
  4. 使用merge標簽:當LinearLayout是根布局時,可以使用減少視圖層級

常見問題解決

Q1:為什么設置了weight但視圖不顯示?
A:確保對應的width/height設置為0dp

Q2:如何讓最后一個按鈕靠右對齊?
A:可以在按鈕前添加一個空白View:

<Viewandroid:layout_width="0dp"android:layout_height="1dp"android:layout_weight="1"/>
<Buttonandroid:layout_width="wrap_content"android:layout_height="wrap_content"android:text="靠右按鈕"/>

Q3:如何實現wrap_content但又限制最大寬度?
A:使用android:maxWidth屬性或結合ConstraintLayout

本文來自互聯網用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務,不擁有所有權,不承擔相關法律責任。
如若轉載,請注明出處:http://www.pswp.cn/diannao/82855.shtml
繁體地址,請注明出處:http://hk.pswp.cn/diannao/82855.shtml
英文地址,請注明出處:http://en.pswp.cn/diannao/82855.shtml

如若內容造成侵權/違法違規/事實不符,請聯系多彩編程網進行投訴反饋email:809451989@qq.com,一經查實,立即刪除!

相關文章

LVS+keepalived實戰案例

目錄 部署LVS 安裝軟件 創建VIP 創建保存規則文件 給RS添加規則 驗證規則 部署RS端 安裝軟件 頁面內容 添加VIP 配置系統ARP 傳輸到rs-2 客戶端測試 查看規則文件 實現keepalived 編輯配置文件 傳輸文件給backup 修改backup的配置文件 開啟keepalived服務 …

(C語言)超市管理系統(測試版)(指針)(數據結構)(二進制文件讀寫)

目錄 前言&#xff1a; 源代碼&#xff1a; product.h product.c fileio.h fileio.c main.c 代碼解析&#xff1a; fileio模塊&#xff08;文件&#xff08;二進制&#xff09;&#xff09; 寫文件&#xff08;保存&#xff09; 函數功能 代碼逐行解析 關鍵知識點 讀文…

ubuntu----100,常用命令2

目錄 文件與目錄管理系統信息與管理用戶與權限管理網絡配置與管理軟件包管理打包與壓縮系統服務與任務調度硬件信息查看系統操作高級工具開發相關其他實用命令 在 Ubuntu 系統中&#xff0c;掌握常用命令可以大幅提升操作效率。以下是一些常用的命令&#xff0c;涵蓋了文件管理…

WiFi密碼查看器打開軟件自動獲取數據

相信有很大一部分人都不知道怎么看已經連過的WiFi密碼。 你還在手動查詢自己的電腦連接過得WiFi密碼嗎&#xff1f; —————【下 載 地 址】——————— 【本章單下載】&#xff1a;https://drive.uc.cn/s/dbbedf933dad4 【百款黑科技】&#xff1a;https://ucnygalh6…

開目新一代MOM:AI賦能高端制造的破局之道

導讀 INTRODUCTION 在高端制造業智能化轉型的深水區&#xff0c;企業正面臨著個性化定制、多工藝場景、動態生產需求的敏捷響應以及傳統MES柔性不足的考驗……在此背景下&#xff0c;武漢開目信息技術股份有限公司&#xff08;簡稱“開目軟件”&#xff09;正式發布新一代開目…

Android開發-視圖基礎

在Android應用開發中&#xff0c;視圖&#xff08;View&#xff09;是構建用戶界面的基本元素。無論是按鈕、文本框還是復雜的自定義控件&#xff0c;它們都是基于View類或其子類實現的。掌握視圖的基礎知識對于創建功能強大且美觀的應用至關重要。本文將深入探討Android中的視…

無人機信號線被電磁干擾導致停機

問題描述&#xff1a; 無人機飛控和電調之間使用PWM信號控制時候&#xff0c;無人機可以正常起飛&#xff0c;但是在空中懸停的時候會出現某一個電機停機&#xff0c;經排查電調沒有啟動過流過壓等保護&#xff0c;定位到電調和飛控之間的信號線被干擾問題。 信號線被干擾&am…

VSCode設置SSH免密登錄

引言 2025年05月13日20:21:14 原來一直用的PyCharn來完成代碼在遠程服務器上的運行&#xff0c;但是PyCharm時不時同步代碼會有問題。因此&#xff0c;嘗試用VSCode來完成代碼SSH遠程運行。由于VSCode每次進行SSH連接的時候都要手動輸入密碼&#xff0c;為了解決這個問題在本…

硬密封保溫 V 型球閥:恒溫工況下復雜介質控制的性價比之選-耀圣

硬密封保溫 V 型球閥&#xff1a;恒溫工況下復雜介質控制的性價比之選 在瀝青儲運、化學原料加工、食品油脂輸送等工業領域&#xff0c;帶顆粒高粘度介質與料漿的恒溫輸送一直是生產的關鍵環節。普通閥門在應對此類介質時&#xff0c;常因溫度流失導致介質凝結堵塞、密封失效&…

最終一致性和強一致性

最終一致性和強一致性是分布式系統中兩種不同的數據一致性模型&#xff0c;它們在數據同步的方式和適用場景上有顯著區別&#xff1a; 1. 強一致性&#xff08;Strong Consistency&#xff09; 定義&#xff1a;所有節點&#xff08;副本&#xff09;的數據在任何時刻都保持一…

基于單應性矩陣變換的圖像拼接融合

單應性矩陣變換 單應性矩陣是一個 3x3 的可逆矩陣&#xff0c;它描述了兩個平面之間的投影變換關系。在圖像領域&#xff0c;單應性矩陣可以將一幅圖像中的點映射到另一幅圖像中的對應點&#xff0c;前提是這兩幅圖像是從不同視角拍攝的同一平面場景。 常見的應用場景&#x…

如何同步虛擬機文件夾

以下是一些常見的同步虛擬機文件夾的方法&#xff1a; 使用共享文件夾&#xff08;以VMware和VirtualBox為例&#xff09; - VMware&#xff1a;打開虛擬機&#xff0c;選擇“虛擬機”->“設置”&#xff0c;在“選項”中選擇“共享文件夾”&#xff0c;點擊“添加”選擇…

前端流行框架Vue3教程:15. 組件事件

組件事件 在組件的模板表達式中&#xff0c;可以直接使用$emit方法觸發自定義事件 觸發自定義事件的目的是組件之間傳遞數據 我們來創建2個組件。父組件&#xff1a; ComponentEvent.vue,子組件&#xff1a;Child.vue Child.vue <script> export default {// 子組件通…

Python+1688 API 開發教程:實現商品實時數據采集的完整接入方案

在電商行業競爭日益激烈的當下&#xff0c;掌握商品實時數據是企業制定精準營銷策略、優化供應鏈管理的關鍵。1688 作為國內重要的 B2B 電商平臺&#xff0c;其開放平臺提供了豐富的 API 接口&#xff0c;借助 Python 強大的數據處理能力&#xff0c;我們能夠高效實現商品數據的…

聊一聊Electron中Chromium多進程架構

Chromium 多進程架構概述 Chromium 的多進程架構是其核心設計之一&#xff0c;旨在提高瀏覽器的穩定性、安全性和性能。Chromium 將不同的功能模塊分配到獨立的進程中&#xff0c;每個進程相互隔離&#xff0c;避免了單進程架構中一個模塊的崩潰導致整個瀏覽器崩潰的問題。 在…

CodeBuddy 中國版 Cursor 實戰:Redis+MySQL雙引擎驅動〈王者榮耀〉戰區排行榜

文章目錄 一、引言二、系統架構設計2.1、整體架構概覽2.2、數據庫設計2.3、后端服務設計 三、實戰&#xff1a;從零構建排行榜3.1、開發環境準備3.2、用戶與戰區 數據管理3.2.1、MySQL 數據庫表創建3.2.2、實現用戶和戰區數據的 CURD 操作 3.3、實時分數更新3.4、排行榜查詢3.5…

Oracle OCP認證考試考點詳解083系列15

題記&#xff1a; 本系列主要講解Oracle OCP認證考試考點&#xff08;題目&#xff09;&#xff0c;適用于19C/21C,跟著學OCP考試必過。 71. 第71題&#xff1a; 題目 解析及答案&#xff1a; 關于在 Oracle 18c 及更高版本中基于 Oracle 黃金鏡像的安裝&#xff0c;以下哪…

LS-NET-012-TCP的交互過程詳解

LS-NET-012-TCP的交互過程詳解 附加&#xff1a;TCP如何保障數據傳輸 TCP的交互過程詳解 一、TCP協議核心交互流程 TCP協議通過三次握手建立連接、數據傳輸、四次揮手終止連接三大階段實現可靠傳輸。整個過程通過序列號、確認應答、窗口控制等機制保障傳輸可靠性。 1.1 三次…

【Pandas】pandas DataFrame cumprod

Pandas2.2 DataFrame Computations descriptive stats 方法描述DataFrame.abs()用于返回 DataFrame 中每個元素的絕對值DataFrame.all([axis, bool_only, skipna])用于判斷 DataFrame 中是否所有元素在指定軸上都為 TrueDataFrame.any(*[, axis, bool_only, skipna])用于判斷…

C語言之旅5---分支與循環【2】

&#x1f4ab;只有認知的突破&#x1f4ab;才來帶來真正的成長&#x1f4ab;編程技術的學習&#x1f4ab;沒有捷徑&#x1f4ab;一起加油&#x1f4ab; &#x1f341;感謝各位的觀看&#x1f341;歡迎大家留言&#x1f341;咱們一起加油&#x1f341;努力成為更好的自己&#x…