RelativeLayout(相對佈局)

来源:http://www.cnblogs.com/rocking7189/archive/2016/07/18/5682626.html
-Advertisement-
Play Games

相對佈局由其名稱大致可以猜測其是相對於其他的控制項進行佈局的。因此呢!其屬性也就比較多了,不過基本上都是有跡可循的。下麵就其屬性值的特點可以將一些常用的屬性分為以下三個類別。 1. 屬性值為true或false(相對於父控制項的位置) android:layout_centerHrizontal 水平居 ...


相對佈局由其名稱大致可以猜測其是相對於其他的控制項進行佈局的。因此呢!其屬性也就比較多了,不過基本上都是有跡可循的。下麵就其屬性值的特點可以將一些常用的屬性分為以下三個類別。

 

1. 屬性值為truefalse(相對於父控制項的位置)

android:layout_centerHrizontal

水平居中

android:layout_centerVertical

垂直居中

android:layout_centerInparent

相對於父元素完全居中

android:layout_alignParentBottom

貼緊父元素的下邊緣

android:layout_alignParentLeft

貼緊父元素的左邊緣

android:layout_alignParentRight

貼緊父元素的右邊緣

android:layout_alignParentTop

貼緊父元素的上邊緣

android:layout_alignWithParentIfMissing

如果對應的兄弟元素找不到的話就以父元素做參照物

 

2. 屬性值必須為id的引用名"@id/id_name"

android:layout_below

在某元素的下方

android:layout_above

在某元素的的上方

android:layout_toLeftOf

在某元素的左邊

android:layout_toRightOf

在某元素的右邊

android:layout_alignTop

本元素的上邊緣和某元素的上邊緣對齊

android:layout_alignLeft

本元素的左邊緣和某元素的左邊緣對齊

android:layout_alignBottom

本元素的下邊緣和某元素的下邊緣對齊

android:layout_alignRight

本元素的右邊緣和某元素的右邊緣對齊

 

3. 屬性值為具體的像素值

android:layout_marginBottom

本元素底邊緣離某元素的距離

android:layout_marginLeft

本元素左邊緣離某元素的距離

android:layout_marginRight

本元素右邊緣離某元素的距離

android:layout_marginTop

本元素上邊緣離某元素的距離

 

當然,除了這些個屬性,像之前在LinearLayout中用到的android:backgroundandroid:gravityandroid:layout_gravity等等屬性該佈局也都有用到。同樣的,還是通過代碼來加深對一些屬性的認識

<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/relativeLayout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#acdefb">

    <Button
        android:id="@+id/tv0"
        android:layout_width="80dp"
        android:layout_height="wrap_content"
        android:background="#FF0000"
        android:layout_centerInParent="true"
        android:textSize="24sp"
        android:maxLines="2"
        android:text="中間相對組件"/>

    <Button
        android:id="@+id/tv1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:background="#012345"
        android:layout_above="@+id/tv0"
        android:layout_toLeftOf="@+id/tv0"
        android:layout_alignParentLeft="true"
        android:layout_marginRight="30dp"
        android:layout_marginBottom="30dp"
        android:text="隱約雷鳴,陰霾天空"/>

    <Button
        android:id="@+id/tv2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:background="#465892"
        android:layout_above="@+id/tv0"
        android:layout_toRightOf="@+id/tv0"
        android:layout_alignParentRight="true"
        android:layout_marginLeft="30dp"
        android:layout_marginBottom="30dp"
        android:text="但盼風雨來,能留君在此"/>

    <Button
        android:id="@+id/tv3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:background="#458698"
        android:layout_below="@+id/tv0"
        android:layout_toLeftOf="@+id/tv0"
        android:layout_alignParentLeft="true"
        android:layout_marginRight="30dp"
        android:layout_marginTop="30dp"
        android:text="隱約雷鳴,陰霾天空"/>

    <Button
        android:id="@+id/tv4"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:background="#864745"
        android:layout_below="@+id/tv0"
        android:layout_toRightOf="@+id/tv0"
        android:layout_alignParentRight="true"
        android:layout_marginLeft="30dp"
        android:layout_marginTop="30dp"
        android:text="即使天無雨,我亦留此地"/>

</RelativeLayout>

 

在上述代碼中,首先創建一個Button組件tv0,處於父控制項的中間的位置。然後使其他的四個Button相對於tv0的位置來設置其位置。具體為:左上角的tv1,將其設置為處於中間tv0的上邊和左邊,且距離其左邊和上邊30dp,以及貼緊父控制項的左邊緣;右上角的tv2,將其設置為處於中間tv0的上邊和右邊,且距離其右邊和上邊30dp,以及貼緊父控制項的右邊緣;左下角的tv3,將其設置為處於中間tv0的下邊和左邊,且距離其左邊和下邊30dp,以及貼緊父控制項的左邊緣;右下角的tv4,將其設置為處於中間tv0的下邊和右邊,且距離右邊和下邊30dp,以及貼緊父控制項的右邊緣。

 

RelativeLayout的疊加效果,如果不指定每個組件的相對位置,那麼就會都疊加在左上角,有點類似於FrameLayout的疊加效果。當然,一般是指定:android:layout_marginTop、android:layout_marginBottom、    android:layout_marginLeft、android:layout_marginRight四個屬性的屬性值為負值來實現。依然是用代碼來檢測其疊加效果:

主要是講上述五個組件的寬度和高度改為200dp,然後將上述四個屬性的屬性值改為負值

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/relativeLayout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#acdefb">

    <Button
        android:id="@+id/tv0"
        android:layout_width="150dp"
        android:layout_height="150dp"
        android:background="#FF0000"
        android:layout_centerInParent="true"
        android:textSize="24sp"
        android:maxLines="2"
        android:text="中間相對組件"/>

    <Button
        android:id="@+id/tv1"
        android:layout_width="150dp"
        android:layout_height="150dp"
        android:background="#012345"
        android:layout_above="@+id/tv0"
        android:layout_toLeftOf="@+id/tv0"
        android:layout_alignParentLeft="true"
        android:layout_marginRight="-50dp"
        android:layout_marginBottom="-50dp"
        android:text="隱約雷鳴,陰霾天空"/>

    <Button
        android:id="@+id/tv2"
        android:layout_width="150dp"
        android:layout_height="150dp"
        android:background="#465892"
        android:layout_above="@+id/tv0"
        android:layout_toRightOf="@+id/tv0"
        android:layout_alignParentRight="true"
        android:layout_marginLeft="-120dp"
        android:layout_marginBottom="-50dp"
        android:text="但盼風雨來,能留君在此"/>

    <Button
        android:id="@+id/tv3"
        android:layout_width="150dp"
        android:layout_height="150dp"
        android:background="#458698"
        android:layout_below="@+id/tv0"
        android:layout_toLeftOf="@+id/tv0"
        android:layout_alignParentLeft="true"
        android:layout_marginRight="-120dp"
        android:layout_marginTop="-50dp"
        android:text="隱約雷鳴,陰霾天空"/>

    <Button
        android:id="@+id/tv4"
        android:layout_width="150dp"
        android:layout_height="150dp"
        android:background="#864745"
        android:layout_below="@+id/tv0"
        android:layout_toRightOf="@+id/tv0"
        android:layout_alignParentRight="true"
        android:layout_marginLeft="-50dp"
        android:layout_marginTop="-50dp"
        android:text="即使天無雨,我亦留此地"/>

</RelativeLayout>

 

可以看到其疊加效果如上圖所示:

只能是相對處在後面定義的組件疊加在前面的組件上,而不能是相對於前面的組件疊加在後邊的組件上。此處的相對前後表示的是後一個組件的位置是相對前面的組件位置而定的

 

其次:要用來做後邊組件相對的組件應該在前邊進行定義,以免找不到組件造成錯誤


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

-Advertisement-
Play Games
更多相關文章
  • 水平居中 若為行內元素,對其父元素用text-align:center即可; 若為塊元素(無浮動),則一般有兩種方法可實現對其的水平居中,一為margin:0 auto;二為通過css計算函數calc; 垂直居中 若為行內元素,一般對其父元素用line-height:{height}即可,若為圖片元 ...
  • 第0章 關於本書 1, 本書要用到一個工具函數————$$(),它可以讓我們更容易獲取和遍歷所有匹配特定css選擇符的dom元素: 2, 以下實現一個效果: 3, 檢查屬性是否存在: 檢測多個屬性: 檢測某個具體的屬性值是否支持: 第1章 前言 1, 不用-ms-border-radius和-o-b ...
  • 路由 通常HTTP URL的格式是這樣的: http://host[:port][path] http表示協議。 host表示主機。 port為埠,可選欄位,不提供時預設為80。 path指定請求資源的URI(Uniform Resource Identifier,統一資源定位符),如果URL中沒 ...
  • 寫在開篇: 浮動屬性的設計初衷,只是為了實現文本環繞效果! 時刻牢記這一點,才能正確使用浮動。 浮動元素的特征: 1.浮動元素脫離文檔流。 2.浮動元素周圍的外邊距不會合併。 3.浮動元素具有包裹性。 4.浮動元素具有破壞性。 下麵這段代碼,能夠驗證上述的四個特征,按提示操作即可。 1 <!DOCT ...
  • 一,代碼。 二,輸出。 ...
  • iOS學習(C語言)知識點整理 一、整型數組 1)概念:一組類型相同,空間連續分佈的數據。 2)int a[10] int 型裡面有10個元素,數組的名字叫a, a還是數組的首地址 。 3)引用數組中的元素 a[i]:數組名+下標,下標從0開始 。 4)數組元素個數剛好與數組長度相當叫完全初始化。 ...
  • 1.效果圖 2.創建頁面文件(main.xml) 1 <Spinner 2 android:id="@+id/spinner1" 3 android:layout_width="match_parent" 4 android:layout_height="wrap_content" /> 5 6 < ...
  • 04
    04 ...
一周排行
    -Advertisement-
    Play Games
  • Dapr Outbox 是1.12中的功能。 本文只介紹Dapr Outbox 執行流程,Dapr Outbox基本用法請閱讀官方文檔 。本文中appID=order-processor,topic=orders 本文前提知識:熟悉Dapr狀態管理、Dapr發佈訂閱和Outbox 模式。 Outbo ...
  • 引言 在前幾章我們深度講解了單元測試和集成測試的基礎知識,這一章我們來講解一下代碼覆蓋率,代碼覆蓋率是單元測試運行的度量值,覆蓋率通常以百分比表示,用於衡量代碼被測試覆蓋的程度,幫助開發人員評估測試用例的質量和代碼的健壯性。常見的覆蓋率包括語句覆蓋率(Line Coverage)、分支覆蓋率(Bra ...
  • 前言 本文介紹瞭如何使用S7.NET庫實現對西門子PLC DB塊數據的讀寫,記錄了使用電腦模擬,模擬PLC,自至完成測試的詳細流程,並重點介紹了在這個過程中的易錯點,供參考。 用到的軟體: 1.Windows環境下鏈路層網路訪問的行業標準工具(WinPcap_4_1_3.exe)下載鏈接:http ...
  • 從依賴倒置原則(Dependency Inversion Principle, DIP)到控制反轉(Inversion of Control, IoC)再到依賴註入(Dependency Injection, DI)的演進過程,我們可以理解為一種逐步抽象和解耦的設計思想。這種思想在C#等面向對象的編 ...
  • 關於Python中的私有屬性和私有方法 Python對於類的成員沒有嚴格的訪問控制限制,這與其他面相對對象語言有區別。關於私有屬性和私有方法,有如下要點: 1、通常我們約定,兩個下劃線開頭的屬性是私有的(private)。其他為公共的(public); 2、類內部可以訪問私有屬性(方法); 3、類外 ...
  • C++ 訪問說明符 訪問說明符是 C++ 中控制類成員(屬性和方法)可訪問性的關鍵字。它們用於封裝類數據並保護其免受意外修改或濫用。 三種訪問說明符: public:允許從類外部的任何地方訪問成員。 private:僅允許在類內部訪問成員。 protected:允許在類內部及其派生類中訪問成員。 示 ...
  • 寫這個隨筆說一下C++的static_cast和dynamic_cast用在子類與父類的指針轉換時的一些事宜。首先,【static_cast,dynamic_cast】【父類指針,子類指針】,兩兩一組,共有4種組合:用 static_cast 父類轉子類、用 static_cast 子類轉父類、使用 ...
  • /******************************************************************************************************** * * * 設計雙向鏈表的介面 * * * * Copyright (c) 2023-2 ...
  • 相信接觸過spring做開發的小伙伴們一定使用過@ComponentScan註解 @ComponentScan("com.wangm.lifecycle") public class AppConfig { } @ComponentScan指定basePackage,將包下的類按照一定規則註冊成Be ...
  • 操作系統 :CentOS 7.6_x64 opensips版本: 2.4.9 python版本:2.7.5 python作為腳本語言,使用起來很方便,查了下opensips的文檔,支持使用python腳本寫邏輯代碼。今天整理下CentOS7環境下opensips2.4.9的python模塊筆記及使用 ...