LinerLayout與RelativeLayout的需要注意的一同異同

一、共同點

padding 指的是該容器到邊距的距離填充多大,padding四周填充,對應的看單詞便很明了,不再翻譯還有paddingLeft、 paddingRinght、 paddingBottom、 paddingTop、paddingStart、 paddingEnd
margin 兩個邊距之間的距離 Margin 四周邊距的距離,對應的還有 MarginLeft、MarginRinght、MarginBottom、MarginTop、MarginStart、MarginEnd
layout_gravity 控件本身的位置 center 在布局的重心位置 centerHorizontal 水平方向居中 centerVertical 垂直方向居中 bottom 、 top、 left、 right、 fill垂直與水平方向均充滿、 fill_horizotal 水平方向充滿、 fill_vetical垂直方向充滿、 clip_Horizontal、 clip_Vertical、 end、 start
gravity 控件中內容的位置 center 子控件父控件的重心位置,其它屬性值為:centerHorizontal 、 centerVertical、 bottom、 top、 left 、 ringht、 fill fill_horizotal、 fill_vetical、 clip_Horizontal 、 clip_Vertical、 end 、 start

注意:若布局為LinerLayout,當LinerLayout的oritation= “vertical”時,那么layout_gravity垂直方向如:centerVertiacl 、bottom、top、fill_vetical、clip_vertical的屬性就失效了;同理,當LinerLayout=“horizon”時,centerHorizontal、fill_horizotal、clip_Horizontal等失效。

二、LinerLayout獨有

只能沿某一水平或者垂直方向排列,故經常以嵌套方式實現稍微靈活一點的排列布局。
oritation=“vertical”沿垂直方向排列
oritation=“horizon”沿水平方向排列
layout_weight 比重 一般用于等距分割各個控件間的距離,而不是根據某個屏幕的寬度或高度計算出控件間的距離,這樣到尺寸不同的屏幕后就不適應了。
layout_weight 的值越大,所占的比重越大。

注意:如果要使控件在布局中水平等距排列,這時控件的layout_weight="0dp",layout_weitht="1"; 如果要使控件在布局中垂直等距排列,這時控件的layout_height="0dp",layout_weitht="1"

三、RelativeLayout獨有

布局比較靈活,不需要多層嵌套,降低LinerLayout的嵌套層級。
主要用于布局是非線性排列的情況,要為每個控件加上layout_id="@id/x"屬性,然后再確定另一個控件是相對于那個控件的哪個位置,屬性主要有以下所列出的:
a對于b的位置:layout_toEndOf 、 layout_toLeftOf、 layout_toRightOf、 layout_toStartOf 、 layout_bellow、 layout_above
沿著父布局的位置layout_alignParentRight --在父布局的右邊邊距對齊、layout_alignParentTop沿著父布局的頂端邊距對齊、 layout_alignBaseline layout_alignParenEnd 、 layout_alignParentLeft、 layout_alignParentStart layout_alignRight
沿著x控件的邊距對齊:layout_alignLeft--在x控件的左邊距對齊、 layout_alignStart 、 layout_alignStart、 layout_alignTop、 layout_alignEnd 、 layout_alignLeft

四、應用以上這些屬性已經足以應付一些比較簡單的布局分布了,下面是一個實例:

<?xml version="1.0" encoding="utf-8"?>
 <RelativeLayout     xmlns:android="http://schemas.android.com/apk/res/android"                          android:orientation="vertical"
  android:layout_width="match_parent"
  android:layout_height="match_parent"
   android:layout_marginLeft="20dp"
   android:layout_marginRight="20dp">
  <View
    android:id="@+id/r1"
    android:layout_marginTop="20dp"
    android:layout_width="match_parent"
    android:layout_height="50dp"
    android:background="@color/colorPrimary"/> 
<View
    android:id="@+id/r2"
    android:layout_below="@+id/r1"
    android:layout_marginTop="20dp"
    android:layout_width="match_parent"
    android:layout_height="50dp"
    android:background="@color/colorPrimary"/>
<View
    android:id="@+id/r3"
    android:layout_below="@+id/r2"
    android:layout_marginTop="20dp"
    android:layout_width="60dp"
    android:layout_height="20dp"
    android:background="@color/colorAccent"/> <View
    android:id="@+id/r4"
    android:layout_below="@+id/r2"
    android:layout_alignParentRight="true"
    android:layout_marginTop="20dp"
    android:layout_width="60dp"
    android:layout_height="20dp"
    android:background="@color/colorAccent"/><LinearLayout
    android:layout_below="@+id/r3"
    android:layout_width="match_parent"
    android:layout_height="100dp"
    android:layout_marginTop="70dp" >
<View
    android:id="@+id/r6"
    android:layout_width="0dp"
    android:layout_height="match_parent"
    android:layout_weight="1"
    android:background="@color/colorPrimaryDark"/>
<View
    android:id="@+id/r7"
    android:layout_width="0dp"
    android:layout_marginLeft="10dp"
    android:layout_height="100dp"
    android:layout_weight="1"
    android:background="@color/colorPrimaryDark"/>
<View
    android:id="@+id/r8"
    android:layout_width="0dp"
    android:layout_marginLeft="10dp"
    android:layout_height="100dp"
    android:layout_weight="1"                                                        
    android:background="@color/colorPrimaryDark"/>
 </LinearLayout>
 </RelativeLayout>
image.png
最后編輯于
?著作權歸作者所有,轉載或內容合作請聯系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發布,文章內容僅代表作者本人觀點,簡書系信息發布平臺,僅提供信息存儲服務。

推薦閱讀更多精彩內容

  • 看了幾篇文章以及自己寫了一些,關于布局的問題,根據別人的寫作總結一下得到 一.基本理論Android六大基本布局分...
    shuaikun閱讀 832評論 0 4
  • ¥開啟¥ 【iAPP實現進入界面執行逐一顯】 〖2017-08-25 15:22:14〗 《//首先開一個線程,因...
    小菜c閱讀 6,523評論 0 17
  • Android功能強大,界面華麗,但是眾多的布局屬性就害苦了開發者,下面這篇文章結合了網上不少資料.第一類:屬性值...
    HangChen閱讀 4,937評論 0 24
  • !!!注意drawImage的寫法,其傳參值份三種,不同的寫法,表示不同的功能,其傳參的個數也不一樣:
    royluck閱讀 119評論 0 0
  • 又到快開學的季節,上周六我們睡在床上聊天的時候還在說,暑假快結束了,要上班啦! 然后寶貝說,暑假已經結束了,今天是...
    請叫我通通閱讀 280評論 1 0