五月综合缴情婷婷六月,色94色欧美sute亚洲线路二,日韩制服国产精品一区,色噜噜一区二区三区,香港三级午夜理伦三级三

您現(xiàn)在的位置: 365建站網(wǎng) > 365文章 > Android開發(fā)重點:RelativeLayout(相對布局)詳解

Android開發(fā)重點:RelativeLayout(相對布局)詳解

文章來源:365jz.com     點擊數(shù):1331    更新時間:2018-05-27 23:05   參與評論

      和線性布局(LinearLayout)一樣,RelaiveLayout相對布局也是我們用的比較多的一個布局之一。相對,顧名思義是有參照的,就是以某個兄弟組件,或者父容器來決定的(兄弟組件是在一個同一個布局里面的組件,如果是布局里一個組件參照另一個布局里的組件會出錯)。合理地利用好LinearLayout的weight權(quán)重屬性和RelativeLayout相 對布局,可以解決屏幕分辨率不同的自適應(yīng)問題。

  比如小明在上學(xué)的路上,此時他的位置可以用離家多少米或者是離學(xué)校多少米表示,就是利用不同的參照物。

  好了,廢話不多說,直接說比較常用的屬性吧~

使用RelativeLayout的話,可能僅僅需要一層就可以完成了,以父容器或者兄弟組件參考+margin +padding就可以設(shè)置組件的顯示位置,是比較方便的!當(dāng)然,也不是絕對的,具體問題具體分析吧! 總結(jié)就是:盡量使用RelativeLayout + LinearLayout的weight屬性搭配使用吧!
 

設(shè)置布局里面所有組件的對其方式:

android:gravity:設(shè)置容器內(nèi)各個子組件的對齊方式

android:ignoreGravity:如果為哪個組件設(shè)置了這個屬性的話,那么該組件不受gravity屬性的影響

根據(jù)父容器來定位:

想位于哪,哪個屬性就設(shè)置為true

左對齊:android:layout_alighParentLeft

右對齊:android:layout_alighParentRight

頂端對齊:android:layout_alighParentTop

底部對齊:android:layout_alighParentBottom

水平居中:android:layout_centerHorizontal

垂直居中:android:layout_centerVertical

中央位置:android:layout_centerInParent


1.核心屬性圖


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


3.根據(jù)兄弟組件定位

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

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

運行效果圖:

實現(xiàn)代碼:

<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的區(qū)別

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

比較示例代碼如下:

<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> 

運行效果圖比較:


5.很常用的一點:margin可以設(shè)置為負數(shù)

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

效果圖如下:

此處輸入圖片的描述

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

<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>  

上一張圖~(有點丑......大家湊合看~)

根據(jù)兄弟組件來定位(右面的屬性值為兄弟組件的id

 

 

左邊:android:layout_toLeftOf

右邊:android:layout_toRightOf

上方:android:layout_above

下方:android:layout_below

對齊上邊界:android:layout_alignTop

對齊下邊界:android:layout_alignBottom

對齊左邊界:android:layout_alignLeft

對齊右邊界:android:layout_alignRight

 

這里演示一個比較典型的例子~

梅花布局:

相關(guān)代碼如下:

<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>

 

最后還有兩個比較常用的Margin和Padding屬性!

Margin:設(shè)置組件與父容器(通常是布局)的邊距

android:layout_margin: 指定控件的四周的外部留出一定的邊距
android:layout_marginLeft: 指定控件的左邊的外部留出一定的邊距
android:layout_marginTop: 指定控件的上邊的外部留出一定的邊距
android:layout_marginRight: 指定控件的右邊的外部留出一定的邊距
android:layout_marginBottom: 指定控件的下邊的外部留出一定的邊距

Padding:設(shè)置組件內(nèi)部元素間的邊距(可以理解為填充)

android:padding :指定控件的四周的內(nèi)部留出一定的邊距
android:paddingLeft: 指定控件的左邊的內(nèi)部留出一定的邊距
android:paddingTop: 指定控件的上邊的內(nèi)部留出一定的邊距
android:paddingRight: 指定控件的右邊的內(nèi)部留出一定的邊距
android:paddingBottom: 指定控件的下邊的內(nèi)部留出一定的邊距

這兩個后面都跟著一個參數(shù),通常用dp作為單位,eg:android:margin = "10dp"

效果圖如下:

相關(guān)代碼:

<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"    
    />

代碼解釋:

這個代碼很簡單,就是寫了兩個按鈕的組合。

第一個組合的第二個按鈕設(shè)置了paddingleft = "100dp:,結(jié)果按鈕被拉伸了100dp,因為里面的元素間距填充了100dp;

第二個組合的第二個按鈕設(shè)置了marginleft = "100dp",結(jié)果按鈕向右平移了100dp。

 

總結(jié)

 

RelativeLayout用到的一些重要的屬性:

第一類:屬性值為true或false

android:layout_centerHrizontal 水平居中 
android:layout_centerVertical 垂直居中 
android:layout_centerInparent 相對于父元素完全居中 
android:layout_alignParentBottom 貼緊父元素的下邊緣 
android:layout_alignParentLeft 貼緊父元素的左邊緣 
android:layout_alignParentRight 貼緊父元素的右邊緣 
android:layout_alignParentTop 貼緊父元素的上邊緣 
android:layout_alignWithParentIfMissing 如果對應(yīng)的兄弟元素找不到的話就以父元素做參照物

第二類:屬性值必須為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 本元素的右邊緣和某元素的的右邊緣對齊

第三類:屬性值為具體的像素值,如30dip,40px

android:layout_marginBottom 離某元素底邊緣的距離 
android:layout_marginLeft 離某元素左邊緣的距離 
android:layout_marginRight 離某元素右邊緣的距離 
android:layout_marginTop 離某元素上邊緣的距離

EditText的android:hint

  設(shè)置EditText為空時輸入框內(nèi)的提示信息。

android:gravity 

  android:gravity屬性是對該view 內(nèi)容的限定.比如一個button 上面的text. 你可以設(shè)置該text 在view的靠左,靠右等位置.以button為例,android:gravity="right"則button上面的文字靠右

android:layout_gravity

  android:layout_gravity是用來設(shè)置該view相對與起父view 的位置.比如一個button 在linearlayout里,你想把該button放在靠左、靠右等位置就可以通過該屬性設(shè)置.以button為例,android:layout_gravity="right"則button靠右

android:layout_alignParentRight

  使當(dāng)前控件的右端和父控件的右端對齊。這里屬性值只能為true或false,默認(rèn)false。

android:scaleType:

  android:scaleType是控制圖片如何resized/moved來匹對ImageView的size。

  ImageView.ScaleType / android:scaleType值的意義區(qū)別:

CENTER /Center/?.jpg 按圖片的原來size居中顯示,當(dāng)圖片長/寬超過View的長/寬,則截取圖片的居中部分顯示;

CENTER_CROP / centerCrop 按比例擴大圖片的size居中顯示,使得圖片長(寬)等于或大于View的長(寬);

CENTER_INSIDE / centerInside 將圖片的內(nèi)容完整居中顯示,通過按比例縮小或原來的size使得圖片長/寬等于或小于View的長/寬;

FIT_CENTER / fitCenter 把圖片按比例擴大/縮小到View的寬度,居中顯示;

FIT_END / fitEnd 把圖片按比例擴大/縮小到View的寬度,顯示在View的下部分位置;

FIT_START / fitStart 把圖片按比例擴大/縮小到View的寬度,顯示在View的上部分位置;

FIT_XY / fitXY 把圖片不按比例擴大/縮小到View的大小顯示;

MATRIX / matrix 用矩陣來繪制,動態(tài)縮小放大圖片來顯示。

  要注意一點,Drawable文件夾里面的圖片命名是不能大寫的。

LinearLayout和RelativeLayout 比較

LinearLayout和RelativeLayout

共有屬性:
java代碼中通過btn1關(guān)聯(lián)次控件
android:id="@+id/btn1"

控件寬度
android:layout_width="80px" //"80dip"或"80dp"
android:layout_width =“wrap_content”
android:layout_width =“match_parent”

控件高度
android:layout_height="80px" //"80dip"或"80dp"
android:layout_height =“wrap_content”
android:layout_height =“match_parent”

控件排布
android:orientation="horizontal”
android:orientation="vertical“

控件間距
android:layout_marginLeft="5dip" //距離左邊
android:layout_marginRight="5dip" //距離右邊
android:layout_marginTop="5dip" //距離上面
android:layout_marginRight="5dip" //距離下面

android:paddingLeft="5dip"


控件顯示位置
android:gravity="center" //left,right, top, bottom
android:gravity="center_horizontal"

android:layout_gravity是本元素對父元素的重力方向。
android:layout_gravity屬性則設(shè)置控件本身相對于父控件的顯示位置
android:gravity是本元素所有子元素的重力方向。

android:layout_gravity="center_vertical"
android:layout_gravity="left"
android:layout_gravity="left|bottom"


TextView中文本字體
android:text="@String/text1" //在string.xml中定義text1的值
android:textSize="20sp"
android:textColor=”#ff123456”
android:textStyle="bold" //普通(normal), 斜體(italic),粗斜體(bold_italic)

TextView中,控制其以...結(jié)束

android:ellipsize="end"

只有一行

android:singleLine="true"

定義控件是否可見
android:visibility=”visible” //可見
android:visibility=”invisible”  //不可見,但是在布局中占用的位置還在
android:visibility=”gone”   //不可見,完全從布局中消失

定義背景圖片
android:background="@drawable/img_bg" //img_bg為drawable下的一張圖片

seekbar控件背景圖片及最大值
android:progressDrawable="@drawable/seekbar_img"
android:thumb="@drawable/thumb"    
android:max = "60"

android:layout_alignWithParentIfMissing="true"

僅在RelativeLayout中有效:
在父親布局的相對位置
android:layout_alignParentLeft="true" //在布局左邊
android:layout_alignParentRight="true" //在布局右邊
android:layout_alignParentTop="true" //在布局上面
android:layout_alignParentBottom="true " //在布局的下面

在某個控件的相對位置
android:layout_toRightOf="@id/button1" //在控件button1的右邊,不僅僅是緊靠著
android:layout_toLeftOf="@id/button1" //在控件button2的左邊,不僅僅是緊靠著
android:layout_below="@id/button1 " //在控件button1下面,不僅僅是正下方
android:layout_above=“@id/button1” //在控件button1下面,不僅僅是正下方

定義和某控件對奇
android:layout_alignTop=”@id/button1” //和控件button1上對齊
android:layout_alignBottom=”@id/button1” //和控件button1下對齊
android:layout_alignLeft=”@id/button1” //和控件button1左對齊
android:layout_alignRight=”@id/button1” //和控件button2右對齊


android:layout_centerHorizontal="true"   //水平居中
android:layout_centerVertical="true"
android:layout_centerInParent="true"

僅在LinearLayout中有效
設(shè)置控件在一排或一列中所占比例值
android:layout_weight="1"


如對本文有疑問,請?zhí)峤坏浇涣髡搲?,廣大熱心網(wǎng)友會為你解答??! 點擊進入論壇

發(fā)表評論 (1331人查看,0條評論)
請自覺遵守互聯(lián)網(wǎng)相關(guān)的政策法規(guī),嚴(yán)禁發(fā)布色情、暴力、反動的言論。
昵稱:
最新評論
------分隔線----------------------------

其它欄目

· 建站教程
· 365學(xué)習(xí)

業(yè)務(wù)咨詢

· 技術(shù)支持
· 服務(wù)時間:9:00-18:00
365建站網(wǎng)二維碼

Powered by 365建站網(wǎng) RSS地圖 HTML地圖

copyright © 2013-2024 版權(quán)所有 鄂ICP備17013400號