Android Relative Layout 安卓相對佈局詳解

来源:https://www.cnblogs.com/Pushy/archive/2018/08/17/9495449.html
-Advertisement-
Play Games

" " 思維導圖可在 "幕布" 找到 1. 基礎 如果在相對佈局里,控制項沒有指明相對位置,則預設都是在相對佈局的左上角: gravity 屬性用來設置容器內組件的對齊方式 效果為 2. 根據兄弟控制項定位 2.1 相對兄弟組件的位置 代碼示例 等屬性通過制定控制項的 來選擇需要參考的兄弟組件,即 : 顯 ...


Android Relative Layout.png

思維導圖可在幕布找到

1. 基礎

如果在相對佈局里,控制項沒有指明相對位置,則預設都是在相對佈局的左上角:

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:background="#FF00FF"
    android:padding="20dp"
    android:text="Item2"/>

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:background="#FF0000"
    android:text="Item1"/>

TIM截圖20180817200955.png

gravity

gravity屬性用來設置容器內組件的對齊方式

<TextView
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:gravity="center"
    android:background="#FF4081"
    android:text="Item1" />

效果為

TIM截圖20180817205322.png

2. 根據兄弟控制項定位

2.1 相對兄弟組件的位置

android:layout_above:// 參考的兄弟控制項上邊
android:layout_below:// 參考的兄弟控制項下邊
android:layout_toLeftOf // 參考的兄弟控制項的左邊
android:layout_toRightOf // 參考的兄弟控制項右邊

代碼示例

android:layout_below等屬性通過制定控制項的id來選擇需要參考的兄弟組件,即@id/firstText

<TextView
    android:id="@+id/firstText"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:background="#FF0000 "
    android:text="firstText" />

<TextView
    android:id="@+id/rightText"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:background="#00FF00"
    android:layout_toRightOf="@id/firstText"
    android:text="rightText" />

<TextView
    android:id="@+id/bottomText"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:background="#00FF00"
    android:layout_below="@id/firstText"
    android:text="bottomText" />

顯示的效果為

TIM截圖20180817202110.png

2.2 對齊相對組件

對齊兄弟相對組件的有四個屬性,為android:layout_align${方向}

android:layout_alignTop // 對齊參考組件的上邊界
android:layout_alignBottom // 對齊參考組件的下邊界
android:layout_alignLeft // 對齊參考組件的左邊界
android:layout_alignRight  // 對齊參考組件的右邊界

android:layout_alignTop等屬性同樣是通過制定控制項的ID來設置參考的組件的邊界線:

代碼示例1

<TextView
    android:id="@+id/item1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:background="#FF00FF"
    android:padding="20dp"
    android:text="Item2"/>

<TextView
    android:id="@+id/item2"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:background="#FF0000"
    android:padding="10dp"
    android:layout_below="@id/item1"
    android:layout_alignRight="@id/item1"
    android:text="Item1"/>

效果為

TIM截圖20180817204032.png

代碼實例2

<TextView
    android:id="@+id/item3"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:background="#FF00FF"
    android:padding="20dp"
    android:text="Item3"/>

<TextView
    android:id="@+id/item4"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:background="#FF0000"
    android:padding="10dp"
    android:layout_toRightOf="@+id/item3"
    android:layout_alignBottom="@id/item3"
    android:text="Item4"/>

效果為

TIM截圖20180817204040.png

3. 根據父控制項定位

3.1 與父控制項的四個邊緣對齊

與父控制項的邊緣對齊的屬性由android:layout_alignParent${方向}組成

android:layout_alignParentTop  // 頂部對齊於父控制項
android:layout_alignParentBottom // 底部對齊於父控制項
android:layout_alignParentLeft // 左對齊於父控制項
android:layout_alignParentRight // 右對齊於父控制項

需要註意的是,這些屬性是通過布爾值來設置是否對齊於父控制項的某個方向的:

<RelativeLayout
    android:layout_width="match_parent"
    android:layout_height="300dp">
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="first"
        android:textSize="50dp"
        android:background="#FF0000"
        android:layout_alignParentRight="true"
        android:layout_alignParentBottom="true"/>
</RelativeLayout>

效果為:

除此之外還有layout_alignParentLeftlayout_alignParentTop

3.2 對齊至父控制項的中央

對齊至父控制項中央的屬性可以用來設置居中的佈局位置:

android:layout_centerHorizontal  // 水平居中
android:layout_centerVertical // 垂直居中
android:layout_centerInParent // 水平且垂直居中,處於父組件的正中央位置

代碼示例

同樣,這些屬性也是通過設置的值也是布爾類型:

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="centerHorizontal"
    android:textSize="50dp"
    android:background="#FF0000"
    android:layout_centerHorizontal="true"/>

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="centerVertical"
    android:textSize="50dp"
    android:background="#FF0000"
    android:layout_centerVertical="true"/>
    
<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="centerInParent"
    android:textSize="15dp"
    android:background="#0000FF"
    android:layout_centerInParent="true"/>

效果為:

4. 其他

對齊至控制項的基準線

<TextView
    android:id="@+id/firstText"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:textSize="100dp"
    android:text="Hello" />

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_toRightOf="@+id/firstText"
    android:text="World" />

如果沒有使用對齊基準線,那麼當Hello的字體的大於world時,world則無法和hello在同一基準線上:

通過給world的TextView添加layout_alignBaseline屬性來實現對齊firstText控制項的基準線:

android:layout_alignBaseline="@+id/firstText"

效果為:

5. 實例

用相對佈局來完成經典的梅花佈局

    <!--中央-->
    <ImageView
        android:id="@+id/img1"
        android:layout_width="80dp"
        android:layout_height="80dp"
        android:layout_centerInParent="true"
        android:src="@drawable/pic1"/>

    <!--右邊-->
    <ImageView
        android:layout_width="80dp"
        android:layout_height="80dp"
        android:layout_centerInParent="true"
        android:layout_toLeftOf="@+id/img1"
        android:layout_centerVertical="true"
        android:src="@drawable/pic2"/>

    <!--左邊-->
    <ImageView
        android:layout_width="80dp"
        android:layout_height="80dp"
        android:layout_toRightOf="@+id/img1"
        android:layout_centerVertical="true"
        android:src="@drawable/pic3"/>

    <!--上邊-->
    <ImageView
        android:layout_width="80dp"
        android:layout_height="80dp"
        android:layout_above="@+id/img1"
        android:layout_centerHorizontal="true"
        android:src="@drawable/pic4"/>

    <!--下邊-->
    <ImageView
        android:layout_width="80dp"
        android:layout_height="80dp"
        android:layout_below="@+id/img1"
        android:layout_centerHorizontal="true"
        android:src="@drawable/pic5"/>

效果圖為

參考資料

2.2.2 RelativeLayout(相對佈局)


您的分享是我們最大的動力!

-Advertisement-
Play Games
更多相關文章
  • 創建一個測試數據表 對UNION 和UNION ALL 進行比較: 在MySQL 5.1中文手冊中有下麵一句話: 如果您對UNION不使用關鍵詞ALL,則所有返回的行都是唯一的,如同您已經對整個結果集合使用了DISTINCT。如果您指定了ALL,您會從所有用過的SELECT語句中得到所有匹配的行。 ...
  • <if test="type ==1"> and DATE_FORMAT(create_date,'%Y-%m-%d') = DATE_FORMAT(now(),'%Y-%m-%d') </if> <if test="type ==2"> <![CDATA[ and DATE_FORMAT(crea ...
  • 基礎準備:Node.Js 、npm或cnpm、redis安裝 1、建立一個項目文件夾,這裡命名 wxfc ,打開命令行輸入 npm install redis 、 因為沒有創建package.json所以警告, 但仍然可以用這個包的。 package.json只是展示項目所依賴的npm包的描述文件。 ...
  • 解壓版安裝操作官網下載: mysql-installer-community-8.0.12.0.msi如上操作: http://www.cnblogs.com/elfin/p/9429877.htmlmysqld --install 安裝服務net start mysql 啟動服務 解決navica ...
  • redis cluster + sentinel詳細過程和錯誤處理三主三備三哨兵1、基本架構192.168.70.215 7001 Master + sentinel 27001192.168.70.216 7002 Master + sentinel 27002192.168.70.217 700 ...
  • 抓到視窗期,分享紅利,所有為快不破!!! 當年學習移動互聯網的程式員現在年薪都50萬了,抓住機遇創業的都成功了 如今會多種主流後端技術的複合型人才已成為市場標配,這就是Java大數據 Java開發、大數據 人才缺口達到20萬以上,每年以20%的速度在增長 後端伺服器開發最流行的是Java開發,而開發 ...
  • 一. 查詢緩存 1.開啟緩存 設置了緩存開啟,緩存最大限制128M,重啟服務後,再次查詢 2 測試緩存 現在是緩存2次,命中一次 上面是二個查詢sql語句,此時緩存數是4,如下圖所示: 此時緩存數是6,說明緩存區分where條件值的大小寫。同樣也會區分sql關鍵詞的大小寫。如下圖所示: 設置好que ...
  • 什麼是SYS_OP_C2C呢?官方的介紹如下: SYS_OP_C2C is an internal function which does an implicit conversion of varchar2 to national character set using TO_NCHAR func... ...
一周排行
    -Advertisement-
    Play Games
  • 前言 本文介紹一款使用 C# 與 WPF 開發的音頻播放器,其界面簡潔大方,操作體驗流暢。該播放器支持多種音頻格式(如 MP4、WMA、OGG、FLAC 等),並具備標記、實時歌詞顯示等功能。 另外,還支持換膚及多語言(中英文)切換。核心音頻處理採用 FFmpeg 組件,獲得了廣泛認可,目前 Git ...
  • OAuth2.0授權驗證-gitee授權碼模式 本文主要介紹如何筆者自己是如何使用gitee提供的OAuth2.0協議完成授權驗證並登錄到自己的系統,完整模式如圖 1、創建應用 打開gitee個人中心->第三方應用->創建應用 創建應用後在我的應用界面,查看已創建應用的Client ID和Clien ...
  • 解決了這個問題:《winForm下,fastReport.net 從.net framework 升級到.net5遇到的錯誤“Operation is not supported on this platform.”》 本文內容轉載自:https://www.fcnsoft.com/Home/Sho ...
  • 國內文章 WPF 從裸 Win 32 的 WM_Pointer 消息獲取觸摸點繪製筆跡 https://www.cnblogs.com/lindexi/p/18390983 本文將告訴大家如何在 WPF 裡面,接收裸 Win 32 的 WM_Pointer 消息,從消息裡面獲取觸摸點信息,使用觸摸點 ...
  • 前言 給大家推薦一個專為新零售快消行業打造了一套高效的進銷存管理系統。 系統不僅具備強大的庫存管理功能,還集成了高性能的輕量級 POS 解決方案,確保頁面載入速度極快,提供良好的用戶體驗。 項目介紹 Dorisoy.POS 是一款基於 .NET 7 和 Angular 4 開發的新零售快消進銷存管理 ...
  • ABP CLI常用的代碼分享 一、確保環境配置正確 安裝.NET CLI: ABP CLI是基於.NET Core或.NET 5/6/7等更高版本構建的,因此首先需要在你的開發環境中安裝.NET CLI。這可以通過訪問Microsoft官網下載並安裝相應版本的.NET SDK來實現。 安裝ABP ...
  • 問題 問題是這樣的:第三方的webapi,需要先調用登陸介面獲取Cookie,訪問其它介面時攜帶Cookie信息。 但使用HttpClient類調用登陸介面,返回的Headers中沒有找到Cookie信息。 分析 首先,使用Postman測試該登陸介面,正常返回Cookie信息,說明是HttpCli ...
  • 國內文章 關於.NET在中國為什麼工資低的分析 https://www.cnblogs.com/thinkingmore/p/18406244 .NET在中國開發者的薪資偏低,主要因市場需求、技術棧選擇和企業文化等因素所致。歷史上,.NET曾因微軟的閉源策略發展受限,儘管後來推出了跨平臺的.NET ...
  • 在WPF開發應用中,動畫不僅可以引起用戶的註意與興趣,而且還使軟體更加便於使用。前面幾篇文章講解了畫筆(Brush),形狀(Shape),幾何圖形(Geometry),變換(Transform)等相關內容,今天繼續講解動畫相關內容和知識點,僅供學習分享使用,如有不足之處,還請指正。 ...
  • 什麼是委托? 委托可以說是把一個方法代入另一個方法執行,相當於指向函數的指針;事件就相當於保存委托的數組; 1.實例化委托的方式: 方式1:通過new創建實例: public delegate void ShowDelegate(); 或者 public delegate string ShowDe ...