天天看點

Android常用布局:線性布局和相對布局2.相對布局:RelativeLayout

1.線性布局:LinearLayout

一定要在父布局檔案寫orientation(控件排列順序)屬性:

垂直排列:vertical (豎)

水準排列:horizontal(橫)

<LinearLayout 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:orientation="vertical"
</LinearLayout>
           

常用屬性:

android:gravity:

用于設定該控件中内容相對于該控件的對齊方式

android:layout_gravity:

用于設定該控件相對于父控件的對齊方式

android:padding:

用于設定該控件中内容相對于該控件的邊距,即内邊距。

android:layout_margin:

用于設定該控件相對于其他控件的邊距,即外邊距。

layout_weight

用于線上性布局中指定父控件剩餘空間的配置設定比例。

水準方向的線性布局中:使用weight時,需注意将寬度設定為0dp

垂直方向的線性布局中:使用weight時,需注意将高度設定為0dp

2.相對布局:RelativeLayout

相對布局也是一種常用的布局,和線性布局的排列方式不同,相對布局是通過相對定位的方式讓控件出現在布局的任何位置

android:layout_below屬性作用是使該控件處于另一個控件的下方android:layout_toRightOf屬性作用是使該控件位于另一個控件的右邊,相對布局就是這樣通過各種屬性來排列布局的位置

一,屬性值為true或false:

layout_centerHrizontal 水準居中

layout_centerVertical 垂直居中

layout_centerInparent 相對于父元素完全居中

layout_alignParentBottom 貼緊父元素的下邊緣

layout_alignParentLeft 貼緊父元素的左邊緣

layout_alignParentRight 貼緊父元素的右邊緣

layout_alignParentTop 貼緊父元素的上邊緣

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

二,屬性值必須為id的引用名“@id/id-name”

layout_below 在某元素的下方

layout_above 在某元素的的上方

layout_toLeftOf 在某元素的左邊

layout_toRightOf 在某元素的右邊

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

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

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

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

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

layout_marginBottom 離某元素底邊緣的距離

layout_marginLeft 離某元素左邊緣的距離

layout_marginRight 離某元素右邊緣的距離

layout_marginTop 離某元素上邊緣的距離