一、共同點
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>