Android移動應(yīng)用開發(fā)指南之六種布局詳解
LinearLayout
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:dividerPadding="200dp"
>
<LinearLayout
android:layout_width="100dp"
android:layout_height="0dp"
android:background="#ff0000"
android:layout_weight="1"
/>
<View
android:layout_width="match_parent"
android:layout_height="1dp"
android:background="#ff000000"
/>
<LinearLayout
android:layout_width="100dp"
android:layout_height="0dp"
android:background="#ffff00"
android:layout_weight="1"
/>
<LinearLayout
android:layout_width="200dp"
android:layout_height="00dp"
android:layout_weight="1"
android:background="#00ffff" />
</LinearLayout>
orientation設(shè)置排列方式
layout_weight設(shè)置權(quán)重(感覺和彈性盒子差不多)

RelativeLayout
顧名思義,相對元素布局
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_height="match_parent"
android:layout_width="match_parent"
android:padding="10dp"
>
<RelativeLayout
android:id="@+id/rl1"
android:layout_width="100dp"
android:layout_height="100dp"
android:background="#ff0000"
android:layout_centerInParent="true"
/>
<RelativeLayout
android:layout_margin="0dp"
android:layout_width="100dp"
android:layout_height="100dp"
android:background="#00ff00"
android:layout_toLeftOf="@+id/rl1"
/>
</RelativeLayout>

FrameLayout
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:android="http://schemas.android.com/apk/res/android">
<FrameLayout
android:layout_width="400dp"
android:layout_height="400dp"
android:background="#ff0000"
/>
<FrameLayout
android:layout_width="300dp"
android:layout_height="300dp"
android:background="#ffff00"
android:foreground="@drawable/a"
/>
<FrameLayout
android:layout_width="200dp"
android:layout_height="200dp"
android:background="#00ff00"/>
</FrameLayout>

簡單來說,就是可以疊一起的布局
TableLayout
<?xml version="1.0" encoding="utf-8"?>
<TableLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:collapseColumns=""
android:layout_width="match_parent"
android:layout_height="match_parent"
>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="第1個"
/>
<TableRow>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="第一個"
/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="第二個"
/>
</TableRow>
<TableRow>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="第一個"
/>
</TableRow>
</TableLayout>

可以看成類似excel的表格一樣的布局
通常結(jié)合< TableRow >一起使用
GridLayout
網(wǎng)格布局
<?xml version="1.0" encoding="utf-8"?>
<GridLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal"
android:columnCount="3"
>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_row="0"
android:layout_column="1"
android:text="第一個"
/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="第二個"
/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="第三個"
android:layout_columnSpan="3"
/>
</GridLayout>

可以看成TableLayout升級版?
ConstraintLayout
約束布局
這個應(yīng)該是最強(qiáng)的布局了
創(chuàng)建布局默認(rèn)的就是這個了。

打開design模式,然后隨便拖幾個按鈕進(jìn)去

點擊魔術(shù)棒建立約束。
ok完成布局了。
代碼也自動生成好了:
<?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">
<Button
android:id="@+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="246dp"
android:layout_marginTop="107dp"
android:text="按鈕"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<Button
android:id="@+id/button3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginEnd="172dp"
android:layout_marginBottom="125dp"
android:text="Button"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent" />
<Button
android:id="@+id/button4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="115dp"
android:text="Button"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
我們開個虛擬機(jī)運(yùn)行一下:

只能說,差不太多,微調(diào)一下差不多就能用了。
也能夠設(shè)置各個組件的屬性值顏色字體等等。
這用起來就像是墨刀一樣。
參考
https://www.bilibili.com/video/BV1Jb4y187C4/?p=25&spm_id_from=pageDriver&vd_source=f57738ab6bbbbd5fe07aae2e1fa1280f
總結(jié)
到此這篇關(guān)于Android移動應(yīng)用開發(fā)指南之六種布局的文章就介紹到這了,更多相關(guān)Android移動應(yīng)用開發(fā)布局內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
android客戶端從服務(wù)器端獲取json數(shù)據(jù)并解析的實現(xiàn)代碼
今天總結(jié)一下android客戶端從服務(wù)器端獲取json數(shù)據(jù)的實現(xiàn)代碼,需要的朋友可以參考下2013-06-06
Android中的Dalvik和ART詳解及區(qū)別分析
小編通過這篇文章給大家整理了什么是Dalvik和ART,并進(jìn)行了區(qū)別的分析,下面一起來看看。2016-07-07
AndroidManifest.xml <uses-feature>和<uses-permisstio
這篇文章主要介紹了AndroidManifest.xml <uses-feature>和<uses-permisstion>分析及比較的相關(guān)資料,需要的朋友可以參考下2017-06-06

