【Android】約束布局總結(1)

在這里插入圖片描述
三三要成為安卓糕手

零:創建布局文件方式

1:創建步驟

外鏈圖片轉存失敗,源站可能有防盜鏈機制,建議將圖片保存下來直接上傳

ctrl + alt + 空格 設置根元素

外鏈圖片轉存失敗,源站可能有防盜鏈機制,建議將圖片保存下來直接上傳

外鏈圖片轉存失敗,源站可能有防盜鏈機制,建議將圖片保存下來直接上傳

2:處理老版本約束布局

在一些老的工程中,constrainlayout可能沒有辦法被直接使用,這里需要手動添加依賴

外鏈圖片轉存失敗,源站可能有防盜鏈機制,建議將圖片保存下來直接上傳

implementation 'androidx.constraintlayout:constraintlayout:2.2.1'

這里的版本很多,需要斟酌

外鏈圖片轉存失敗,源站可能有防盜鏈機制,建議將圖片保存下來直接上傳

這里的implementation libs.constraintlayout點進去其實就是一個聲明;

參數解釋:group理解成組織標識;name是依賴庫的具體名稱;最后一個是版本引用。我們也可以自己去定義一個依賴聲明,如下圖中的test,并加以運用

外鏈圖片轉存失敗,源站可能有防盜鏈機制,建議將圖片保存下來直接上傳

文件已被改變是否以最新的為主,synchronized現在就同步

外鏈圖片轉存失敗,源站可能有防盜鏈機制,建議將圖片保存下來直接上傳

總結:擴展庫、第三方庫可以幫助我們更好的開發,但不是必備的

一:約束布局源代碼

在安卓系統中提供了三種布局方式:LinearLayout(線性布局),RelativeLayout(相對布局),Constrainlayout(約束布局)官方推薦使用第三種布局方式

1:Relative所在位置

解釋:<Android API 34, extension level 7 Platform> 是項目創建的時候就會導入進來的一個api,我們的RelativeLayout類就處于其中

外鏈圖片轉存失敗,源站可能有防盜鏈機制,建議將圖片保存下來直接上傳

2:Constraint所在位置

可以看到我們的約束布局是在第三方工具庫中提供的

外鏈圖片轉存失敗,源站可能有防盜鏈機制,建議將圖片保存下來直接上傳

二:方位約束

1:控件拖拽

所有布局方式都支持控件拖拽,但ConstraintLayout對拖拽的支持最好,

外鏈圖片轉存失敗,源站可能有防盜鏈機制,建議將圖片保存下來直接上傳

拖拽后的空間只支持預覽,在程序運行后,并不會真的實現

tools的用法,相當于預覽作用,運行時不會在Android手機上顯示

如果當前的控件沒有約束條件,就會顯示在(0,0)的位置;

兩點定位:一般需要設置x軸和y軸的坐標的

外鏈圖片轉存失敗,源站可能有防盜鏈機制,建議將圖片保存下來直接上傳

2:方位的控制

默認有這句話,就可以添加一些xml的屬性

外鏈圖片轉存失敗,源站可能有防盜鏈機制,建議將圖片保存下來直接上傳

textview控件的左邊在父控件的左邊

    <TextViewandroid:id="@+id/textView1"android:layout_width="wrap_content"android:layout_height="wrap_content"android:text="TextView"app:layout_constraintLeft_toLeftOf="parent"app:layout_constraintTop_toTopOf="parent" /><TextViewandroid:id="@+id/textView2"android:layout_width="wrap_content"android:layout_height="wrap_content"android:text="TextView"app:layout_constraintRight_toRightOf="parent"app:layout_constraintTop_toTopOf="@+id/root" /><TextViewandroid:id="@+id/textView3"android:layout_width="wrap_content"android:layout_height="wrap_content"android:text="textView"app:layout_constraintBottom_toBottomOf="parent"app:layout_constraintLeft_toLeftOf="parent" /><TextViewandroid:id="@+id/textView4"android:layout_width="wrap_content"android:layout_height="wrap_content"android:text="textView"app:layout_constraintBottom_toBottomOf="@+id/root"app:layout_constraintRight_toRightOf="parent" /><TextViewandroid:id="@+id/textView5"android:layout_width="wrap_content"android:layout_height="wrap_content"android:text="TextView5"app:layout_constraintBottom_toBottomOf="parent"app:layout_constraintLeft_toLeftOf="parent"app:layout_constraintRight_toRightOf="parent"app:layout_constraintTop_toTopOf="parent" />

效果展示

外鏈圖片轉存失敗,源站可能有防盜鏈機制,建議將圖片保存下來直接上傳

同時設置四個方向,就會四馬分尸666,究極拉扯,如textView5

注意

		app:layout_constraintLeft_toLeftOf="parent"app:layout_constraintRight_toRightOf="parent"app:layout_constraintStart_toStartOf="parent"app:layout_constraintEnd_toEndOf="parent"
當閱讀習慣都是從左往右時,可以等價替換

三:同級控件位置約束

理解清楚方位,就可以控制控件與控件之間的相對位置,寫法都是非常靈活的

 <TextViewandroid:id="@+id/textView6"android:layout_width="wrap_content"android:layout_height="100dp"android:background="@color/my_blue"android:text="TextView6"app:layout_constraintLeft_toRightOf="@+id/textView5"app:layout_constraintTop_toBottomOf="@+id/textView5" /><TextViewandroid:id="@+id/textView7"android:layout_width="wrap_content"android:layout_height="wrap_content"android:text="TextView7"app:layout_constraintRight_toLeftOf="@+id/textView5"app:layout_constraintTop_toBottomOf="@+id/textView5" />

外鏈圖片轉存失敗,源站可能有防盜鏈機制,建議將圖片保存下來直接上傳

    <TextViewandroid:id="@+id/textView8"android:layout_width="wrap_content"android:layout_height="wrap_content"android:text="TextView"app:layout_constraintBottom_toBottomOf="@+id/textView6"app:layout_constraintLeft_toRightOf="@+id/textView6"app:layout_constraintTop_toTopOf="@+id/textView6" />

處于中間位置

外鏈圖片轉存失敗,源站可能有防盜鏈機制,建議將圖片保存下來直接上傳

再玩個離譜的兄弟

    <TextViewandroid:layout_width="wrap_content"android:layout_height="wrap_content"android:id="@+id/textView9"android:text="textView9"app:layout_constraintLeft_toLeftOf="@+id/textView6"app:layout_constraintRight_toRightOf="@id/textView6"app:layout_constraintTop_toBottomOf="@id/textView6"app:layout_constraintBottom_toBottomOf="@id/textView6"/>

這里其實是基于控件外部為基準,而非內部的文字

外鏈圖片轉存失敗,源站可能有防盜鏈機制,建議將圖片保存下來直接上傳

四:文本基準線約束

我們現在希望以當前文字100為底線對齊,而不是以控件的外圍做對齊的

RelativeLayout都是基于當前控件大小進行調整的,constraintlayout可以實現這個場景

1:RelativeLayout對齊方式

(1)代碼

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"xmlns:app="http://schemas.android.com/apk/res-auto"xmlns:tools="http://schemas.android.com/tools"android:id="@+id/root"android:layout_width="match_parent"android:layout_height="match_parent"><TextViewandroid:id="@+id/tv_price"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_centerInParent="true"android:background="@color/my_blue"android:text="100"android:textColor="@color/white"android:textSize="56sp" /><TextViewandroid:id="@+id/tv_label"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_centerInParent="true"android:layout_toRightOf="@+id/tv_price"android:background="@color/my_blue"android:text="$"android:textColor="@color/white"android:textSize="28sp" /></RelativeLayout>

(2)效果

RelativeLayout都是基于當前控件大小進行調整的,還是做不到嘛嗚嗚┭┮﹏┭┮

外鏈圖片轉存失敗,源站可能有防盜鏈機制,建議將圖片保存下來直接上傳

(3)重要代碼分析

相對布局居中方式是centerInParent,約束布局是二/四馬拉扯

2:Baseline

有方法

Baseline_toBaselineOf

Baseline_toTopOf 以此類推

(1)關聯文本控件

翻譯為基準線

    <TextViewandroid:id="@+id/tv_price"android:layout_width="wrap_content"android:layout_height="wrap_content"android:background="@color/purple"android:text="100"android:textColor="@color/white"android:textSize="56sp"app:layout_constraintBottom_toBottomOf="parent"app:layout_constraintLeft_toLeftOf="parent"app:layout_constraintRight_toRightOf="parent"app:layout_constraintTop_toTopOf="parent" /><TextViewandroid:id="@+id/tv_label"android:layout_width="wrap_content"android:layout_height="wrap_content"android:background="@color/purple"app:layout_constraintLeft_toRightOf="@+id/tv_price"app:layout_constraintBottom_toBottomOf="@+id/tv_price"app:layout_constraintBaseline_toBaselineOf="@+id/tv_price"android:text="$"android:textColor="@color/white"android:textSize="28sp" />

控件和控件之間能很好的匹配文字底部對齊

外鏈圖片轉存失敗,源站可能有防盜鏈機制,建議將圖片保存下來直接上傳

(2)關聯非文本控件

注意

  • 當使用 toBaselineOf 關聯非文本控件時,ConstraintLayout 會將其基線視為 頂部位置(即 top)。因此,對于非文本控件,****toBaselineOf 的效果等同于 toTopOf
<TextViewandroid:id="@+id/tv_label2"android:layout_width="wrap_content"android:layout_height="wrap_content"android:background="@color/purple"app:layout_constraintLeft_toRightOf="@+id/image_wechat"app:layout_constraintBaseline_toBaselineOf="@+id/image_wechat"android:text="$"android:textColor="@color/white"android:textSize="28sp" /><ImageViewandroid:id="@+id/image_wechat"android:layout_width="100dp"android:layout_height="100dp"android:background="@color/my_blue"android:src="@drawable/icon_wechat"app:layout_constraintBottom_toBottomOf="parent"app:layout_constraintEnd_toEndOf="parent"app:layout_constraintStart_toStartOf="parent"/>

外鏈圖片轉存失敗,源站可能有防盜鏈機制,建議將圖片保存下來直接上傳

五:角度約束

1:angle和radius

    <TextViewandroid:id="@+id/tv_price"android:layout_width="wrap_content"android:layout_height="wrap_content"android:background="@color/purple"android:text="100"android:textColor="@color/white"android:textSize="56sp"app:layout_constraintBottom_toBottomOf="parent"app:layout_constraintLeft_toLeftOf="parent"app:layout_constraintRight_toRightOf="parent"app:layout_constraintTop_toTopOf="parent" /><TextViewandroid:id="@+id/tv_label"android:layout_width="wrap_content"android:layout_height="wrap_content"android:background="@color/purple"android:text="$"android:textColor="@color/white"android:textSize="28sp"app:layout_constraintBottom_toBottomOf="parent"app:layout_constraintCircle="@id/tv_price"app:layout_constraintCircleAngle="45"app:layout_constraintCircleRadius="100dp"app:layout_constraintHorizontal_bias="0.5"app:layout_constraintLeft_toLeftOf="parent" />

外鏈圖片轉存失敗,源站可能有防盜鏈機制,建議將圖片保存下來直接上傳

外鏈圖片轉存失敗,源站可能有防盜鏈機制,建議將圖片保存下來直接上傳

  • Angle 角度45度 可以設置為0~360
  • Radius 半徑(可以理解成兩個控件的中心距離)

盡管添加了xy坐標,但還是以角度約束為準

六:百分比偏移

1:bias

<TextViewandroid:layout_width="wrap_content"android:layout_height="wrap_content"android:background="@color/purple"android:text="我是一個水平偏移和豎直偏移"android:textColor="@color/white"app:layout_constraintBottom_toBottomOf="parent"app:layout_constraintHorizontal_bias="0.1"app:layout_constraintLeft_toLeftOf="parent"app:layout_constraintRight_toRightOf="parent"app:layout_constraintTop_toTopOf="parent"app:layout_constraintVertical_bias="0.2" />

外鏈圖片轉存失敗,源站可能有防盜鏈機制,建議將圖片保存下來直接上傳

總結:只有當兩邊一起拉扯的時候,偏移量才會生效,這里指的就是start和end都設置

可以給0~1的小數參數,默認值為0.5,就是不左不右,在中間;

app:layout_constraintHorizontal_bias="0.1"  水平
app:layout_constraintVertical_bias="0.1"     豎直

七:View的可見性

1:visibility

    <ImageViewandroid:id="@+id/image_wechat"android:layout_width="100dp"android:layout_height="100dp"android:background="@color/my_blue"android:src="@drawable/icon_wechat"android:visibility="visible"app:layout_constraintBottom_toBottomOf="parent"app:layout_constraintEnd_toEndOf="parent"app:layout_constraintStart_toStartOf="parent"app:layout_constraintTop_toTopOf="parent" /><TextViewandroid:id="@+id/tv_label"android:layout_width="wrap_content"android:layout_height="wrap_content"android:background="@color/purple"android:text="$"android:textColor="@color/white"android:textSize="28sp"app:layout_constraintBottom_toTopOf="@id/image_wechat"app:layout_constraintRight_toRightOf="@id/image_wechat"app:layout_goneMarginTop="50dp"app:layout_goneMarginBottom="50dp" />

設置為gone屬性就變成右邊的圖片的效果了

外鏈圖片轉存失敗,源站可能有防盜鏈機制,建議將圖片保存下來直接上傳
外鏈圖片轉存失敗,源站可能有防盜鏈機制,建議將圖片保存下來直接上傳

2:gone

所有的View都有visibility(可見性)屬性

外鏈圖片轉存失敗,源站可能有防盜鏈機制,建議將圖片保存下來直接上傳

參數值是否顯示在界面上是否占用布局空間是否參與布局計算動態設置代碼(Java/Kotlin)
visible? 顯示? 占用? 參與view.setVisibility(View.VISIBLE)
invisible? 隱藏? 占用? 參與view.setVisibility(View.INVISIBLE)
gone? 隱藏? 不占用? 不參與view.setVisibility(View.GON

gone不占用空間就會變成一個點,invisible只是單純看不見,但是最外層的輪廓還在

3:goneMarginTop

變成了一個點之后,此時我們的$符號由于依賴關系也受到了影響,有時候會讓我們看不到控件

        app:layout_constraintBottom_toTopOf="@id/image_wechat"app:layout_constraintRight_toRightOf="@id/image_wechat"app:layout_goneMarginRight="50dp"app:layout_goneMarginBottom="50dp" />

外鏈圖片轉存失敗,源站可能有防盜鏈機制,建議將圖片保存下來直接上傳

這四句代碼非常關鍵

TextView的底部在image的上面,所以只能用遠離底部邊距(goneMarginBottom)表現出來的效果就是往上邊移了

TextView的底部在image的右邊,所以只能用遠離右部邊距(goneMarginRight),表現出來的效果就是往左邊移了

提問:為什么要移動呢?

如果image變成一個點,在左下角,導致依賴image而設置位置的控件位置變化,那不就找不到了嘛~~over

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

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

相關文章

S7-200 SMART 數字量 I/O 組態指南:從參數設置到實戰案例

在工業自動化控制中&#xff0c;PLC 的數字量輸入&#xff08;DI&#xff09;和輸出&#xff08;DO&#xff09;是連接傳感器、執行器與控制系統的 “神經末梢”。西門子 S7-200 SMART 作為一款高性價比的小型 PLC&#xff0c;其數字量 I/O 的靈活組態直接影響系統的穩定性與響…

可調諧激光器原理與設計 【DFB 與 DBR 激光器剖析】

可調諧激光器原理與設計 【DFB 與 DBR 激光器剖析】1. 可調諧激光器的原理與分類簡介2. DFB 與 DBR 激光器結構原理比較2.1 DFB&#xff08;Distributed Feedback Laser&#xff09;激光器2.2 DBR&#xff08;Distributed Bragg Reflector&#xff09;激光器2.3 DFB 激光器與 D…

【前端工程化】前端項目開發過程中如何做好通知管理?

在企業級后臺系統中&#xff0c;通知是保障團隊協作、監控系統狀態和及時響應問題的重要手段。與 C 端產品不同&#xff0c;B 端更關注構建完成、部署狀態、異常報警等關鍵節點的推送機制。 本文主要圍繞通知場景、通知內容、通知渠道、自動化集成等方面展開&#xff0c;適用于…

MySQL 9.4.0創新版發布,AI開始輔助編寫發布說明

2025 年 7 月 22 日&#xff0c;MySQL 9.4.0 正式發布。 作為一個創新版&#xff0c;MySQL 9.4.0 最大的創新應該就是使用 Oracle HeatWave GenAI 作為助手幫助編寫版本發布說明了。難道下一步要開始用 AI 輔助編寫數據庫文檔了&#xff1f; 該版本包含的核心功能更新以及問題修…

基于WebSockets和OpenCV的安卓眼鏡視頻流GPU硬解碼實現

基于WebSockets和OpenCV的安卓眼鏡視頻流GPU硬解碼實現 前些天發現了一個巨牛的人工智能學習網站&#xff0c;通俗易懂&#xff0c;風趣幽默&#xff0c;忍不住分享一下給大家&#xff0c;覺得好請收藏。點擊跳轉到網站。 1. 項目概述 本項目旨在實現一個通過WebSockets接收…

人大金倉 kingbase 連接數太多, 清理數據庫連接數

問題描述 kingbase 連接數太多, 清理數據庫連接數 [rootFCVMDZSZNST25041 ~]# su root [rootFCVMDZSZNST25041 ~]# [rootFCVMDZSZNST25041 ~]# su kingbase [kingbaseFCVMDZSZNST25041 root]$ [kingbaseFCVMDZSZNST25041 root]$ ksql could not change directory to "/r…

SpringMVC相關基礎知識

1. servlet.multipart 大小配置 SpringBoot 文件上傳接口中有 MultipartFile 類型的文件參數,上傳較大文件時報錯: org.springframework.web.multipart.MaxUploadSizeExceededException: Maximum upload size exceeded; nested exception is java.lang.IllegalStateExceptio…

HCIP第一次實驗報告

一.實驗需求及拓撲圖&#xff1a;二.實驗需求分析根據提供的網絡拓撲圖和實驗要求&#xff0c;以下是對實驗需求的詳細分析&#xff1a;R5作為ISP:R5只能進行IP地址配置&#xff0c;其所有接口均配置為公有IP地址。認證方式:R1和R5之間使用PPP的PAP認證&#xff0c;R5為主認證方…

React入門學習——指北指南(第五節)

React 交互性:過濾與條件渲染 在前文我們學習了 React 中事件處理和狀態管理的基礎。本節將聚焦兩個重要的進階技巧 ——條件渲染(根據狀態動態顯示不同 UI)和列表過濾(根據條件篩選數據),這兩者是構建交互式應用的核心能力,能讓界面根據用戶操作呈現更智能的響應。 條…

學習嵌入式的第二十九天-數據結構-(2025.7.16)線程控制:互斥與同步

以下是您提供的文本內容的排版整理版本。我已根據內容主題將其分為幾個主要部分&#xff08;互斥鎖、信號量、死鎖、IPC進程間通信、管道操作&#xff09;&#xff0c;并使用清晰的結構組織信息&#xff1a;代碼片段用代碼塊格式&#xff08;指定語言為C&#xff09;突出顯示。…

COZE官方文檔基礎知識解讀第六期 ——數據庫和知識庫

一&#xff0c;一鍵直連數據上傳&#xff0c;存儲&#xff0c;使用 火山方舟的數據庫和知識庫的核心&#xff0c;都是基于開源的數據庫產品&#xff08;mysql&#xff0c;向量數據庫等&#xff09;&#xff0c;將數據庫交互的邏輯封裝在后端&#xff0c;與前端做耦合&#xff0…

生產環境使用云服務器(centOS)部署和使用MongoDB

部署MongoDB流程1. ?安裝MongoDB?版本選擇建議?CentOS 7?&#xff1a;推薦MongoDB 4.4.x&#xff08;兼容性好&#xff09;?CentOS 8/9?&#xff1a;建議最新穩定版&#xff08;如6.0&#xff09;&#xff0c;需單獨安裝mongodb-database-tools安裝步驟1.添加官方倉庫# 添…

思博倫第二到三層測試儀(打流儀)TestCenter 2U硬件安裝及機箱加電_雙極未來

&#xff08;1&#xff09;安裝板卡&#xff1a;上圖中共 4 個紅色線框&#xff0c;上邊兩個紅色線條框住的是機箱的左右兩側導軌&#xff0c;下邊兩條紅色 線條框住的是板卡拉手條&#xff08;用于承載板卡PCB的金屬板&#xff09;左右兩邊的邊沿。 安裝時將拉手條兩邊的邊沿與…

【華為】筆試真題訓練_20250611

本篇博客旨在記錄自已的筆試刷題的練習&#xff0c;里面注有詳細的代碼注釋以及和個人的思路想法&#xff0c;希望可以給同道之人些許幫助。本人也是小白&#xff0c;水平有限&#xff0c;如果文章中有什么錯誤或遺漏之處&#xff0c;望各位可以在評論區指正出來&#xff0c;各…

新浪微博APP v14.5.0:連接世界的社交媒體平臺

新浪微博APP 是一款廣受歡迎的社交媒體應用程序&#xff0c;憑借其強大的功能和豐富的社交生態&#xff0c;成為用戶獲取信息、表達觀點、互動交流的重要平臺。最新版 v14.5.0 內置了微博助手 v2.3.0&#xff0c;進一步提升了用戶體驗和功能多樣性。 軟件功能 1. 發布微博 用…

靜態枚舉返回(簡單實現字典功能)

枚舉緩存策略的實現與應用 通過靜態Map緩存枚舉類的Class對象&#xff0c;避免每次請求時重復反射加載。核心實現是一個包含枚舉類名與對應Class映射的Registry類&#xff1a; public class EnumRegistry {private static final Map<String, Class<?>> ENUM_MAP …

深分頁性能問題分析與優化實踐

在日常測試工作中&#xff0c;我們經常會遇到分頁查詢接口&#xff0c;例如&#xff1a; GET /product/search?keyword&pageNum1&pageSize10乍看之下&#xff0c;這樣的分頁接口似乎并無性能問題&#xff0c;響應時間也很快。但在一次性能壓測中&#xff0c;我們復現了…

LeetCode——1957. 刪除字符使字符串變好

通過萬歲&#xff01;&#xff01;&#xff01; 題目&#xff1a;給你一個字符串&#xff0c;然后讓你刪除幾個字符串&#xff0c;讓他變成好串&#xff0c;好串的定義就是不要出現連續的3個一樣的字符。思路&#xff1a;首先就是要遍歷字符串。我們將要返回的字符串定義為ret&…

Aerospike與Redis深度對比:從架構到性能的全方位解析

在高性能鍵值存儲領域&#xff0c;Aerospike與Redis是兩款備受關注的產品。Redis以其極致的單機性能和豐富的數據結構成為主流選擇&#xff0c;而Aerospike則憑借分布式原生設計和混合存儲架構在大規模場景中嶄露頭角。本文將從架構設計、數據模型、性能表現、擴展性等核心維度…

Linux命令速查手冊

一、命令格式與輔助工具類別符號/命令示例說明基本格式commandls -a /home命令 選項 參數管道符ls -lless重定向>df -h > disk_usage.txt覆蓋寫入文件>>echo "New" >> notes.txt追加寫入文件2>ls non_exist 2> error.txt錯誤輸出重定向快捷…