溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務條款》

如何使用RelativeLayout布局

發布時間:2021-05-10 17:54:06 來源:億速云 閱讀:199 作者:Leah 欄目:移動開發

本篇文章給大家分享的是有關如何使用RelativeLayout布局,小編覺得挺實用的,因此分享給大家學習,希望大家閱讀完這篇文章后可以有所收獲,話不多說,跟著小編一起來看看吧。

核心屬性圖

如何使用RelativeLayout布局

2.父容器定位屬性示意圖

如何使用RelativeLayout布局

3.根據兄弟組件定位

恩,先說下什么是兄弟組件吧,所謂的兄弟組件就是處于同一層次容器的組件,如圖

如何使用RelativeLayout布局

圖中的組件1,2就是兄弟組件了,而組件3與組件1或組件2并不是兄弟組件,所以組件3不能通過組件1或2來進行定位,比如layout_toleftof = "組件1"這樣是會報錯的!切記!關于這個兄弟組件定位的最經典例子就是"梅花布局"了,下面代碼實現下:

運行效果圖:

如何使用RelativeLayout布局

實現代碼:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  xmlns:tools="http://schemas.android.com/tools"  
  android:id="@+id/RelativeLayout1"  
  android:layout_width="match_parent"  
  android:layout_height="match_parent" >  
  
  <!-- 這個是在容器中央的 -->  
    
  <ImageView  
    android:id="@+id/img1"   
    android:layout_width="80dp"  
    android:layout_height="80dp"  
    android:layout_centerInParent="true"  
    android:src="@drawable/pic1"/>  
    
  <!-- 在中間圖片的左邊 -->  
  <ImageView  
    android:id="@+id/img2"   
    android:layout_width="80dp"  
    android:layout_height="80dp"  
    android:layout_toLeftOf="@id/img1"  
    android:layout_centerVertical="true"  
    android:src="@drawable/pic2"/>  
    
  <!-- 在中間圖片的右邊 -->  
  <ImageView  
    android:id="@+id/img3"   
    android:layout_width="80dp"  
    android:layout_height="80dp"  
    android:layout_toRightOf="@id/img1"  
    android:layout_centerVertical="true"  
    android:src="@drawable/pic3"/>  
    
  <!-- 在中間圖片的上面-->  
  <ImageView  
    android:id="@+id/img4"   
    android:layout_width="80dp"  
    android:layout_height="80dp"  
    android:layout_above="@id/img1"  
    android:layout_centerHorizontal="true"  
    android:src="@drawable/pic4"/>  
    
  <!-- 在中間圖片的下面 -->  
  <ImageView  
    android:id="@+id/img5"   
    android:layout_width="80dp"  
    android:layout_height="80dp"  
    android:layout_below="@id/img1"  
    android:layout_centerHorizontal="true"  
    android:src="@drawable/pic5"/>  
  
</RelativeLayout>

4.margin與padding的區別

初學者對于這兩個屬性可能會有一點混淆,這里區分下:首先margin代表的是偏移,比如marginleft = "5dp"表示組件離容器左邊緣偏移5dp; 而padding代表的則是填充,而填充的對象針對的是組件中的元素,比如TextView中的文字比如為TextView設置paddingleft = "5dp",則是在組件里的元素的左邊填充5dp的空間! margin針對的是容器中的組件,而padding針對的是組件中的元素,要區分開來!下面通過簡單的代碼演示兩者的區別:

比較示例代碼如下:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  xmlns:tools="http://schemas.android.com/tools"  
  android:layout_width="match_parent"  
  android:layout_height="match_parent"  
  android:paddingBottom="@dimen/activity_vertical_margin"  
  android:paddingLeft="@dimen/activity_horizontal_margin"  
  android:paddingRight="@dimen/activity_horizontal_margin"  
  android:paddingTop="@dimen/activity_vertical_margin"  
  tools:context=".MainActivity" >  
  
  <Button  
    android:id="@+id/btn1"   
    android:layout_height="wrap_content"  
    android:layout_width="wrap_content"  
    android:text="Button"/>  
  <Button  
    android:paddingLeft="100dp"   
    android:layout_height="wrap_content"  
    android:layout_width="wrap_content"  
    android:text="Button"  
    android:layout_toRightOf="@id/btn1"/>  
    
  <Button  
    android:id="@+id/btn2"   
    android:layout_height="wrap_content"  
    android:layout_width="wrap_content"  
    android:text="Button"  
    android:layout_alignParentBottom="true"/>  
  <Button  
    android:layout_marginLeft="100dp"   
    android:layout_height="wrap_content"  
    android:layout_width="wrap_content"  
    android:text="Button"  
    android:layout_toRightOf="@id/btn2"   
    android:layout_alignParentBottom="true"/>  
    
</RelativeLayout>

運行效果圖比較:

如何使用RelativeLayout布局

5.很常用的一點:margin可以設置為負數

相信很多朋友都不知道一點吧,平時我們設置margin的時候都習慣了是正數的, 其實是可以用負數的,下面寫個簡單的程序演示下吧,模擬進入軟件后,彈出廣告頁面的,右上角的cancle按鈕的margin則是使用負數的!

效果圖如下:

如何使用RelativeLayout布局

貼出的廣告Activity的布局代碼吧,當然,如果你對這個有興趣的話可以下下demo, 因為僅僅是實現效果,所以代碼會有些粗糙!

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
  xmlns:tools="http://schemas.android.com/tools" 
  android:layout_width="match_parent" 
  android:layout_height="match_parent" 
  tools:context="com.jay.example.relativelayoutdemo.MainActivity"  
  android:background="#00CCCCFF"> 
 
  <ImageView 
    android:id="@+id/imgBack" 
    android:layout_width="200dp" 
    android:layout_height="200dp" 
    android:layout_centerInParent="true" 
    android:background="@drawable/myicon" /> 
 
  <ImageView 
    android:id="@+id/imgCancle" 
    android:layout_width="28dp" 
    android:layout_height="28dp" 
    android:layout_alignRight="@id/imgBack" 
    android:layout_alignTop="@id/imgBack" 
    android:background="@drawable/cancel" 
    android:layout_marginTop="-15dp" 
    android:layout_marginRight="-10dp" /> 
 
</RelativeLayout>

以上就是如何使用RelativeLayout布局,小編相信有部分知識點可能是我們日常工作會見到或用到的。希望你能通過這篇文章學到更多知識。更多詳情敬請關注億速云行業資訊頻道。

向AI問一下細節

免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。

AI

亚洲午夜精品一区二区_中文无码日韩欧免_久久香蕉精品视频_欧美主播一区二区三区美女