Android:常用布局詳解

Github地址:https://github.com/WangFion/android-layout-demo

概述

Android的界面都是由一個個控件在特定的布局約束下按照一定的規(guī)則排列組成的。早期Google官方就給我們提供了最基本的六大布局:

  • LinearLayout(線性布局)
  • RelativeLayout(相對布局)
  • FrameLayout(幀布局)
  • GridLayout(網(wǎng)格布局)
  • AbsoluteLayout(絕對布局)
  • TableLayout(表格布局)

其中AbsoluteLayout使用x、y坐標(biāo)來確定位置已經(jīng)不推薦使用了,TableLayout也可以用LinearLayout或者GridLayout來替代所以也不長使用。

  • ConstraintLayout(約束布局)

2016年Google官方推出新出的布局ConstraintLayout(約束布局),集 LinearLayout(線性布局),RelativeLayout(相對布局),百分比布局等的功能于一身,功能強(qiáng)大,使用靈活??梢栽贏pi9以上的Android系統(tǒng)使用它,它的出現(xiàn)主要是為了解決布局嵌套過多的問題,以靈活的方式定位和調(diào)整小部件。

除此之外,開發(fā)者還可以通過繼承自ViewGroup來自定義自己的布局規(guī)則。

布局詳解

LinearLayout(線性布局)

LinearLayout重點理解下面的兩個屬性:

  • android:orientation
    orientation主要是用來約束其子控件的排列方式,主要有vertical(豎向排列)、horizontal(橫向排列)兩種排列方式。
LinearLayout.png
  • android:weight
    weight也主要是針對其子控件來說的。他的意思是將剩余的空閑空間按照weight的比例分配給子控件,默認(rèn)比例是0。
weight.png

所以在使用中我們一般會把 寬或者高設(shè)置為0dp 以直觀的按照我們設(shè)置的比例來顯示控件。

RelativeLayout(相對布局)

默認(rèn)所有的子控件都顯示在左上角,控件之間是根據(jù)相對關(guān)系來排列的,所以一般都需要為每一個控件都指定id,除非沒有控件依賴于它。

  • 在指定控件的哪一邊:(注意:這些屬性都需要有一個指定的id)
    layout_toRightOf 在指定控件的右邊
    layout_toLeftOf 在指定控件的左邊
    layout_above 在指定控件的上邊
    layout_below 在指定控件的下邊
  • 和指定控件的對齊方式(注意:這些屬性都需要有一個指定的id)
    layout_alignRight 與指定控件右對齊
    layout_alignLeft 與指定控件左對齊
    layout_alignTop 與指定控件上對齊
  • 子控件與父容器的對齊關(guān)系(這些屬性的值為true或false)
    layout_centerInParent 與父容器中間對齊 pairunte
    layout_centerVertical 與父容器豎向中心對齊
    layout_centerHorizontal 與父容器橫向中心對齊
    layout_alignParentLeft 與父容器左邊對齊
    layout_alignParentTop 與父容器上邊對齊
    layout_alignParentRight 與父容器右邊對齊
    layout_alignParentBottom 與父容器下邊對齊
RelativeLayout.png

FrameLayout(幀布局)

幀布局的所有控件都是以層疊的方式排列的,所以后添加的控件有可能會擋住先添加的控件。
layout_gravity(設(shè)置給子控件,調(diào)整控件在容器內(nèi)的重心)

FrameLayout.png

GridLayout(網(wǎng)格布局)

  • 常用屬性
    rowCount:總行數(shù)
    columnCount:總列數(shù)
    layout_row:在網(wǎng)格第幾行
    layout_column:在網(wǎng)格第幾列
    layout_rowWeight:行權(quán)重
    layout_columnWeight:列權(quán)重
    layout_rowSpan:跨行數(shù)
    layout_columnSpan:跨列數(shù)
  • 效果圖
GridLayout.png

上圖的layout_rowWeight和layout_columnWeight均為1,行列都評分空間;“=”的layout_rowSpan=2表示跨兩行,“0”的layout_columnSpan=2表示跨2列。

ConstraintLayout(約束布局)

  • 相對定位
    layout_constraintLeft_toLeftOf
    layout_constraintLeft_toRightOf
    layout_constraintRight_toLeftOf
    layout_constraintRight_toRightOf
    layout_constraintTop_toTopOf
    layout_constraintTop_toBottomOf
    layout_constraintBottom_toTopOf
    layout_constraintBottom_toBottomOf
    layout_constraintBaseline_toBaselineOf
    layout_constraintStart_toEndOf
    layout_constraintStart_toStartOf
    layout_constraintEnd_toStartOf
    layout_constraintEnd_toEndOf

constrainXXX代表的是自己的方位,toXXX代表相對于目標(biāo)控件的方位。例如:layout_constraintLeft_toRightOf="@+id/tv1"表示自己的左邊在tv1的右邊。
其中Baseline是指文本的基線:

baseline.png
  • 角度定位
    app:layout_constraintCircle="@+id/TextView1"
    app:layout_constraintCircleAngle="120"(角度)
    app:layout_constraintCircleRadius="150dp"(距離)
ConstrainCircle.png

TextView2的中心在TextView1的中心的120度,距離為150dp

  • goneMargin
    用于約束的控件可見性被設(shè)置為gone的時候使用的margin值
    layout_goneMarginStart
    layout_goneMarginEnd
    layout_goneMarginLeft
    layout_goneMarginTop
    layout_goneMarginRight
    layout_goneMarginBottom
goneMargin.png
app:layout_goneMarginLeft="10dp"
app:layout_goneMarginTop="10dp"
  • 居中
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent"
  • 偏移
    除了使用layout_marginXXX之外,ConstraintLayout還提供了另外一種偏移的屬性:
//取值范圍為:0~1
app:layout_constraintHorizontal_bias //水平偏移
app:layout_constraintVertical_bias //垂直偏移
  • 寬高比
    當(dāng)寬或高至少有一個尺寸被設(shè)置為0dp時,可以用下面屬性設(shè)置寬高比:
app:layout_constraintDimensionRatio="1:1"http://寬:高=1:1
app:layout_constraintDimensionRatio="H,2:3"http://指的是 高:寬=2:3
app:layout_constraintDimensionRatio="W,2:3"http://指的是 寬:高=2:3
    <TextView
        android:id="@+id/TextView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toLeftOf="@+id/TextView2" />

    <TextView
        android:id="@+id/TextView2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        app:layout_constraintLeft_toRightOf="@+id/TextView1"
        app:layout_constraintRight_toLeftOf="@+id/TextView3"
        app:layout_constraintRight_toRightOf="parent" />

    <TextView
        android:id="@+id/TextView3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        app:layout_constraintLeft_toRightOf="@+id/TextView2"
        app:layout_constraintRight_toRightOf="parent" />
鏈.png

一條鏈的第一個控件是這條鏈的鏈頭,可以在鏈頭中設(shè)置 layout_constraintHorizontal_chainStyle來改變鏈的樣式。chains提供了3種樣式,分別是:
CHAIN_SPREAD —— 展開元素 (默認(rèn));
CHAIN_SPREAD_INSIDE —— 展開元素,但鏈的兩端貼近parent;
CHAIN_PACKED —— 鏈的元素將被打包在一起。

chainStyle.png

如果我們把寬度都設(shè)為0dp,這個時候就可以設(shè)置權(quán)重來創(chuàng)建一個權(quán)重鏈:

layout_constraintHorizontal_weight
layout_ constraintVertical_weight
    <TextView
        android:id="@+id/TextView1"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toLeftOf="@+id/TextView2"
        app:layout_constraintHorizontal_weight="2" />

    <TextView
        android:id="@+id/TextView2"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        app:layout_constraintLeft_toRightOf="@+id/TextView1"
        app:layout_constraintRight_toLeftOf="@+id/TextView3"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintHorizontal_weight="3" />

    <TextView
        android:id="@+id/TextView3"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        app:layout_constraintLeft_toRightOf="@+id/TextView2"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintHorizontal_weight="4" />
weight.png
  • GuideLine
    Guildline像輔助線一樣,在預(yù)覽的時候幫助你完成布局(實際不會顯示在界面上)。
    Guildline的主要屬性:
    orientation: 垂直vertical,水平horizontal
    layout_constraintGuide_begin :開始位置
    layout_constraintGuide_end :結(jié)束位置
    layout_constraintGuide_percent :距離頂部的百分比(orientation = horizontal時則為距離左邊)
    <android.support.constraint.Guideline
        android:id="@+id/guideline1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        app:layout_constraintGuide_begin="50dp"/>

    <android.support.constraint.Guideline
        android:id="@+id/guideline2"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical"
        app:layout_constraintGuide_percent="0.5"/>
guideline.png

Github地址:https://github.com/WangFion/android-layout-demo

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。
禁止轉(zhuǎn)載,如需轉(zhuǎn)載請通過簡信或評論聯(lián)系作者。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容