1鸽疾、線性布局(LinearLayout):按照垂直或者水平方向布局的組件
2吊洼、幀布局(FrameLayout):組件從屏幕左上方布局組件
3、表格布局(TableLayout):按照行列方式布局組件
4制肮、絕對布局(AbsoluteLayout):按照絕對坐標(biāo)來布局組件
5冒窍、相對布局(RelativeLayout):相對其它組件的布局方式
6、約束布局 (ConstraintLayout):按照約束布局組件
線性布局LinearLayout
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".MainActivity">
<LinearLayout
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@android:color/holo_blue_dark"
android:padding="15dp"
android:layout_weight="1"
android:textAlignment="center"
android:text="Text1" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@android:color/darker_gray"
android:padding="15dp"
android:layout_weight="1"
android:textAlignment="center"
android:text="Text2" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@android:color/holo_red_dark"
android:padding="15dp"
android:layout_weight="1"
android:textAlignment="center"
android:text="Text3" />
</LinearLayout>
<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@android:color/holo_orange_light"
android:padding="15dp"
android:textAlignment="center"
android:text="Text4" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@android:color/holo_blue_bright"
android:padding="15dp"
android:textAlignment="center"
android:text="Text5" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@android:color/holo_green_dark"
android:padding="15dp"
android:textAlignment="center"
android:text="Text6" />
</LinearLayout>
</LinearLayout>
線性布局特點豺鼻,同一級組件之間沒有互相依賴综液,按照添加順序依次排列,其中線性布局最重要的屬性是android:orientation儒飒,通過該屬性可以指定水平方向排列還是縱向排列
幀布局FrameLayout
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".MainActivity">
<TextView
android:layout_width="200dp"
android:layout_height="200dp"
android:layout_gravity="center"
android:background="@android:color/holo_green_dark" />
<TextView
android:layout_width="150dp"
android:layout_height="150dp"
android:layout_gravity="center"
android:background="@android:color/holo_blue_bright" />
<TextView
android:layout_width="100dp"
android:layout_height="100dp"
android:layout_gravity="center"
android:background="@android:color/holo_orange_light" />
</FrameLayout>
幀布局會按照添加順序?qū)盈B在一起谬莹,默認(rèn)層疊在左上角位置,本例子因為設(shè)置了layout_gravity="center"桩了,所以層疊在視圖中心位置
表格布局TableLayout
<?xml version="1.0" encoding="utf-8"?>
<TableLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@android:color/holo_green_dark"
android:shrinkColumns="0,2"
>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="我占據(jù)一行" />
<TableRow>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="0" >
</Button>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="111111111111111111111111" >
</Button>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="222222222222222222222222" >
</Button>
</TableRow>
<TableRow>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:text="000000000000000000000000" >
</Button>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="11" >
</Button>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="222222222222222222222222" >
</Button>
</TableRow>
<TableRow>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="000000000000000000000000" >
</Button>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_span="2"
android:text="我占據(jù)2列" >
<!--android:layout_span表示合并單元格個數(shù)-->
</Button>
</TableRow>
</TableLayout>
TableLayout繼承LinearLayout,有元素單獨占一行可以不寫TableRow
TableLayout的一些屬性:
android:shrinkColumns 設(shè)置可收縮的列
android:stretchColumns 設(shè)置可伸展的列
android:collapseColumns 設(shè)置要隱藏的列
默認(rèn)所有的列為stretchColumns届良,如只有一個控件則獨占一行
每列寬度的計算原則:首先根據(jù)可伸展列根據(jù)其內(nèi)容最長的一行占據(jù)一行的百分比,再根據(jù)可收縮的列等分剩余的部分
例如圖中圣猎,優(yōu)先計算“ 111111111111111111111111”的寬度士葫,再由剩下的列等分剩余的空間
如果遇到空間已經(jīng)不足了,則剩余的列均不顯示
控件的一些屬性:
android:layout_column表示當(dāng)前控件在第幾列
android:layout_span表示合并單元格個數(shù)
絕對布局AbsoluteLayout
難以實現(xiàn)多分辨率適配送悔,不建議使用
相對布局RelativeLayout
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
>
<TextView
android:id="@+id/text1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:background="@android:color/holo_blue_bright"
android:padding="20dp"
android:text="text1"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="@+id/text1"
android:layout_alignLeft="@+id/text1"
android:background="@android:color/holo_green_dark"
android:padding="20dp"
android:text="text2"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/text1"
android:layout_alignLeft="@+id/text1"
android:background="@android:color/holo_orange_light"
android:padding="20dp"
android:text="text3"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toLeftOf="@+id/text1"
android:layout_alignTop="@+id/text1"
android:background="@android:color/holo_red_dark"
android:padding="20dp"
android:text="text4"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toRightOf="@+id/text1"
android:layout_alignTop="@+id/text1"
android:background="@android:color/holo_purple"
android:padding="20dp"
android:text="text5"/>
</RelativeLayout>
相對布局常用屬性:
子類控件相對子類控件:值是另外一個控件的id
android:layout_above----------位于給定DI控件之上
android:layout_below ----------位于給定DI控件之下
android:layout_toLeftOf -------位于給定控件左邊
android:layout_toRightOf ------位于給定控件右邊
android:layout_alignLeft -------左邊與給定ID控件的左邊對齊
android:layout_alignRight ------右邊與給定ID控件的右邊對齊
android:layout_alignTop -------上邊與給定ID控件的上邊對齊
android:layout_alignBottom ----底邊與給定ID控件的底邊對齊
android:layout_alignBaseline----對齊到控件基準(zhǔn)線
android:layout_above="@id/xxx" --將控件置于給定ID控件之上
android:layout_below="@id/xxx" --將控件置于給定ID控件之下
android:layout_toLeftOf="@id/xxx" --將控件的右邊緣和給定ID控件的左邊緣對齊
android:layout_toRightOf="@id/xxx" --將控件的左邊緣和給定ID控件的右邊緣對齊
android:layout_alignLeft="@id/xxx" --將控件的左邊緣和給定ID控件的左邊緣對齊
android:layout_alignTop="@id/xxx" --將控件的上邊緣和給定ID控件的上邊緣對齊
android:layout_alignRight="@id/xxx" --將控件的右邊緣和給定ID控件的右邊緣對齊
android:layout_alignBottom="@id/xxx" --將控件的底邊緣和給定ID控件的底邊緣對齊
android:layout_alignParentLeft="true" --將控件的左邊緣和父控件的左邊緣對齊
android:layout_alignParentTop="true" --將控件的上邊緣和父控件的上邊緣對齊
android:layout_alignParentRight="true" --將控件的右邊緣和父控件的右邊緣對齊
android:layout_alignParentBottom="true" --將控件的底邊緣和父控件的底邊緣對齊
android:layout_centerInParent="true" --將控件置于父控件的中心位置
android:layout_centerHorizontal="true" --將控件置于水平方向的中心位置
android:layout_centerVertical="true" --將控件置于垂直方向的中心位置
相對父容器慢显,值是true或false
android:layout_alignParentLeft ------相對于父靠左
android:layout_alignParentTop-------相對于父靠上
android:layout_alignParentRight------相對于父靠右
android:layout_alignParentBottom ---相對于父靠下
android:layout_centerInParent="true" -------相對于父即垂直又水平居中
android:layout_centerHorizontal="true" -----相對于父即水平居中
android:layout_centerVertical="true" --------相對于父即處置居中
相對于父容器位置:
android:layout_margin="10dp"
android:layout_marginLeft
android:layout_marginRight
android:layout_marginTop
android:layout_marginBottom
版本4.2以上相對布局新屬性
android:layout_alignStart---------------------將控件對齊給定ID控件的頭部
android:layout_alignEnd----------------------將控件對齊給定ID控件的尾部
android:layout_alignParentStart--------------將控件對齊到父控件的頭部
android:layout_alignParentEnd---------------將控件對齊到父控件的尾部
android:layout_marginLeft指該控件距離邊父控件的邊距,
android:paddingLeft指該控件內(nèi)部內(nèi)容欠啤,如文本距離該控件的邊距荚藻。
約束布局ConstraintLayout
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="16dp"
android:layout_marginTop="16dp"
android:padding="5dp"
android:text="手機號"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<TextView
android:id="@+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="16dp"
android:layout_marginTop="16dp"
android:padding="5dp"
android:text="驗證碼"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/textView1" />
<EditText
android:id="@+id/editText1"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginEnd="16dp"
android:ems="10"
android:hint="請輸入手機號"
android:inputType="textPersonName"
app:layout_constraintBaseline_toBaselineOf="@+id/textView1"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toEndOf="@+id/textView1"
app:layout_constraintTop_toTopOf="parent" />
<EditText
android:id="@+id/editText2"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:ems="10"
android:hint="請輸入驗證碼"
android:inputType="textPersonName"
app:layout_constraintBaseline_toBaselineOf="@+id/textView2"
app:layout_constraintStart_toEndOf="@+id/textView2"
/>
<Button
android:id="@+id/button1"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginEnd="16dp"
android:layout_marginStart="10dp"
android:text="獲取驗證碼"
app:layout_constraintBaseline_toBaselineOf="@+id/editText2"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toEndOf="@+id/editText2" />
<Button
android:id="@+id/button2"
android:layout_width="match_parent"
android:layout_height="40dp"
android:layout_marginStart="16dp"
android:layout_marginTop="16dp"
android:layout_marginEnd="16dp"
android:text="開始"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/editText2" />
</androidx.constraintlayout.widget.ConstraintLayout>