Android 常用布局 介紹與使用

讀前思考

學(xué)習(xí)一門技術(shù)或者看一篇文章最好的方式就是帶著問題去學(xué)習(xí)到腥,這樣才能在過程中有茅塞頓開朵逝、燈火闌珊的感覺,記憶也會更深刻乡范。

  1. 有哪些常用的布局配名?
  2. 每一種布局有何特點(diǎn)與不同啤咽?
  3. 布局上如何優(yōu)化?

1. 約束布局 ConstraintLayout

ConstraintLayout 是 Android Studio 2.2 中主要的新增功能之一渠脉,ConstraintLayout 使用約束的方式來指定各個控件的位置和關(guān)系的宇整,它有點(diǎn)類似于 RelativeLayout,但遠(yuǎn)比 RelativeLayout 要更強(qiáng)大芋膘,它可以有效地解決布局嵌套過多的問題鳞青。

本文主要講解通過 xml 的編寫實現(xiàn) ConstraintLayout,如果想要了解拖拽方式为朋,可參考 Android新特性介紹臂拓,ConstraintLayout完全解析

1. 首先用 ConstraintLayout 實現(xiàn)置頂,高自適應(yīng)习寸,寬 match_parent

<TextView
        android:id="@+id/tv_name"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:gravity="center"
        android:text="姓名"
        android:textSize="20sp"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent"
    />

實現(xiàn)效果圖

image

2. 實現(xiàn)一個控件在另一個控件下方

<TextView
        android:id="@+id/tv_mobile"
        android:layout_width="wrap_content"
        android:layout_height="20dp"
        android:text="手機(jī)號"
        android:gravity="center"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toBottomOf="@id/tv_name"
    />

實現(xiàn)效果圖

image

3. 實現(xiàn)控件上下左右居中顯示

<TextView
        android:id="@+id/tv_age"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="年齡"
        android:gravity="center"
        android:textSize="30sp"
        app:layout_constraintHorizontal_weight="3"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintBottom_toBottomOf="parent"
    />

實現(xiàn)效果

image
  1. 通過布局權(quán)重胶惰,實現(xiàn)百分比布局
<TextView
        android:id="@+id/tab0"
        android:layout_width="0dp"
        android:layout_height="50dp"
        android:background="@color/colorPrimary"
        android:gravity="center"
        android:text="tab1"
        android:textColor="@color/colorAccent"
        android:textSize="20sp"
        app:layout_constraintHorizontal_weight="2"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toLeftOf="@+id/tab1" />

    <TextView
        android:id="@+id/tab1"
        android:layout_width="0dp"
        android:layout_height="0dp"
        android:gravity="center"
        android:text="tab2"
        android:textSize="20sp"
        app:layout_constraintHorizontal_weight="1"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintLeft_toRightOf="@+id/tab0"
        app:layout_constraintRight_toLeftOf="@+id/tab2"
        app:layout_constraintTop_toTopOf="@+id/tab0" />

    <TextView
        android:id="@+id/tab2"
        android:layout_width="0dp"
        android:layout_height="0dp"
        android:background="@color/colorAccent"
        android:gravity="center"
        android:text="tab3"
        app:layout_constraintHorizontal_weight="1"
        android:textColor="@color/colorPrimary"
        android:textSize="20sp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintLeft_toRightOf="@+id/tab1"
        app:layout_constraintRight_toLeftOf="@id/tab3"
        app:layout_constraintTop_toTopOf="@+id/tab0" />

    <TextView
        android:id="@+id/tab3"
        android:layout_width="0dp"
        android:layout_height="0dp"
        android:gravity="center"
        android:text="tab4"
        android:textSize="20sp"
        app:layout_constraintHorizontal_weight="3"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintLeft_toRightOf="@+id/tab2"
        app:layout_constraintRight_toLeftOf="@+id/tab4"
        app:layout_constraintTop_toTopOf="@+id/tab0" />

    <TextView
        app:layout_constraintHorizontal_weight="1"
        android:id="@+id/tab4"
        android:layout_width="0dp"
        android:layout_height="0dp"
        android:background="@color/colorAccent"
        android:gravity="center"
        android:text="tab5"
        android:textColor="@color/colorPrimary"
        android:textSize="20sp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintLeft_toRightOf="@+id/tab3"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="@+id/tab0" />

實現(xiàn)效果圖

image

2. 線性布局 LinearLayout

線性布局是按照水平或垂直的順序?qū)⒆釉?可以是控件或布局)依次按照順序排列,每一個元素都位于前面一個元素之后霞溪。線性布局分為兩種:水平方向和垂直方向的布局孵滞。分別通過屬性 android:orientation="vertical" 和 android:orientation="horizontal" 來設(shè)置。 android:layout_weight 表示子元素占據(jù)的空間大小的比例威鹿。

接下來我們來實現(xiàn)如下效果

image

具體代碼實現(xiàn)

<?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:background="#FFFFFF"
    android:orientation="vertical">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal" >
        <EditText
            android:id="@+id/msg"
            android:inputType="number"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="">
        </EditText>
    </LinearLayout>

    // 第二行為 mc m+ m- mr 四個Button構(gòu)成一個水平布局
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal" >
        <Button
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="mc" android:layout_weight="1">
        </Button>
        <Button
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="m+" android:layout_weight="1">
        </Button>
        <Button
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="m-" android:layout_weight="1">
        </Button>
        <Button
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="mr" android:layout_weight="1">
        </Button>
    </LinearLayout>

</LinearLayout>

3. 相對布局 RelativeLayout

RelativeLayout 繼承于 android.widget.ViewGroup剃斧,其按照子元素之間的位置關(guān)系完成布局的轨香,作為 Android 系統(tǒng)五大布局中最靈活也是最常用的一種布局方式忽你,非常適合于一些比較復(fù)雜的界面設(shè)計。

接下來我們來實現(xiàn)如下效果

image

具體代碼如下

<?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">
    <Button
        android:id="@+id/btn1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:layout_centerInParent="true"
        android:text="Button1"
    />
    <Button
        android:id="@+id/btn2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_above="@id/btn1"
        android:layout_toLeftOf="@id/btn1"
        android:text="Button2"
    />
    <Button
        android:id="@+id/btn3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_above="@id/btn1"
        android:layout_toRightOf="@id/btn1"
        android:text="Button3"
    />
    <Button
        android:id="@+id/btn4"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_above="@id/btn2"
        android:layout_toLeftOf="@id/btn3"
        android:layout_toRightOf="@id/btn2"
        android:text="Button4"
    />

</RelativeLayout>

由于我在項目中很少使用下面的布局臂容,就不過多介紹了

4. 表格布局 TableLayout

表格布局科雳,適用于多行多列的布局格式,每個 TableLayout 是由多個 TableRow 組成脓杉,一個 TableRow 就表示 TableLayout 中的每一行糟秘,這一行可以由多個子元素組成。實際上 TableLayout 和 TableRow 都是 LineLayout 線性布局的子類球散。但是 TableRow 的參數(shù) android:orientation 屬性值固定為 horizontal尿赚,且 android:layout_width=MATCH_PARENT,android:layout_height=WRAP_CONTENT蕉堰。所以 TableRow 實際是一個橫向的線性布局凌净,且所以子元素寬度和高度一致。

5. 框架布局 FrameLayout

FrameLayout 是最簡單的布局了屋讶。所有放在布局里的控件冰寻,都按照層次堆疊在屏幕的左上角。后加進(jìn)來的控件覆蓋前面的控件皿渗。
在 FrameLayout 布局里斩芭,定義任何空間的位置相關(guān)的屬性都毫無意義轻腺。控件自動的堆放在左上角划乖,根本不聽你的控制贬养。但是控件本身是可以控制自己內(nèi)部的布局的。

6. AbsoluteLayou 絕對布局

絕對布局中將所有的子元素通過設(shè)置 android:layout_x 和 android:layout_y 屬性迁筛,將子元素的坐標(biāo)位置固定下來煤蚌,即坐標(biāo) (android:layout_x, android:layout_y) ,layout_x 用來表示橫坐標(biāo)细卧,layout_y 用來表示縱坐標(biāo)尉桩。屏幕左上角為坐標(biāo) (0,0),橫向往右為正方贪庙,縱向往下為正方蜘犁。實際應(yīng)用中,這種布局用的比較少止邮,因為 Android 終端一般機(jī)型比較多这橙,各自的屏幕大小。分辨率等可能都不一樣导披,如果用絕對布局屈扎,可能導(dǎo)致在有的終端上顯示不全等。

布局優(yōu)化

  1. 使用 include 標(biāo)簽加載重復(fù)布局
  2. 使用 merge 標(biāo)簽減少布局嵌套
  3. 使用 ViewStub 動態(tài)控制布局顯示

由于篇幅問題就不對上述做具體實例撩匕,自己可以嘗試著實現(xiàn)鹰晨。

文章已經(jīng)讀到末尾了,不知道最初的幾個問題你都會了嗎止毕?如果不會的話模蜡?可以再針對不會的問題進(jìn)行精讀哦!答案都在文中扁凛,相信你肯定可以解決的忍疾!

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末,一起剝皮案震驚了整個濱河市谨朝,隨后出現(xiàn)的幾起案子卤妒,更是在濱河造成了極大的恐慌,老刑警劉巖字币,帶你破解...
    沈念sama閱讀 219,270評論 6 508
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件则披,死亡現(xiàn)場離奇詭異,居然都是意外死亡纬朝,警方通過查閱死者的電腦和手機(jī)收叶,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 93,489評論 3 395
  • 文/潘曉璐 我一進(jìn)店門,熙熙樓的掌柜王于貴愁眉苦臉地迎上來共苛,“玉大人判没,你說我怎么就攤上這事蜓萄。” “怎么了澄峰?”我有些...
    開封第一講書人閱讀 165,630評論 0 356
  • 文/不壞的土叔 我叫張陵嫉沽,是天一觀的道長。 經(jīng)常有香客問我俏竞,道長绸硕,這世上最難降的妖魔是什么? 我笑而不...
    開封第一講書人閱讀 58,906評論 1 295
  • 正文 為了忘掉前任魂毁,我火速辦了婚禮玻佩,結(jié)果婚禮上,老公的妹妹穿的比我還像新娘席楚。我一直安慰自己咬崔,他們只是感情好,可當(dāng)我...
    茶點(diǎn)故事閱讀 67,928評論 6 392
  • 文/花漫 我一把揭開白布烦秩。 她就那樣靜靜地躺著垮斯,像睡著了一般。 火紅的嫁衣襯著肌膚如雪只祠。 梳的紋絲不亂的頭發(fā)上兜蠕,一...
    開封第一講書人閱讀 51,718評論 1 305
  • 那天,我揣著相機(jī)與錄音抛寝,去河邊找鬼熊杨。 笑死,一個胖子當(dāng)著我的面吹牛墩剖,可吹牛的內(nèi)容都是我干的猴凹。 我是一名探鬼主播夷狰,決...
    沈念sama閱讀 40,442評論 3 420
  • 文/蒼蘭香墨 我猛地睜開眼岭皂,長吁一口氣:“原來是場噩夢啊……” “哼舱沧!你這毒婦竟也來了侠鳄?” 一聲冷哼從身側(cè)響起勺馆,我...
    開封第一講書人閱讀 39,345評論 0 276
  • 序言:老撾萬榮一對情侶失蹤葡粒,失蹤者是張志新(化名)和其女友劉穎雁竞,沒想到半個月后纽窟,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體酒来,經(jīng)...
    沈念sama閱讀 45,802評論 1 317
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡啡浊,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 37,984評論 3 337
  • 正文 我和宋清朗相戀三年猾昆,在試婚紗的時候發(fā)現(xiàn)自己被綠了陶因。 大學(xué)時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片。...
    茶點(diǎn)故事閱讀 40,117評論 1 351
  • 序言:一個原本活蹦亂跳的男人離奇死亡垂蜗,死狀恐怖楷扬,靈堂內(nèi)的尸體忽然破棺而出解幽,到底是詐尸還是另有隱情,我是刑警寧澤烘苹,帶...
    沈念sama閱讀 35,810評論 5 346
  • 正文 年R本政府宣布躲株,位于F島的核電站,受9級特大地震影響镣衡,放射性物質(zhì)發(fā)生泄漏霜定。R本人自食惡果不足惜,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 41,462評論 3 331
  • 文/蒙蒙 一廊鸥、第九天 我趴在偏房一處隱蔽的房頂上張望望浩。 院中可真熱鬧,春花似錦惰说、人聲如沸曾雕。這莊子的主人今日做“春日...
    開封第一講書人閱讀 32,011評論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽剖张。三九已至,卻和暖如春揩环,著一層夾襖步出監(jiān)牢的瞬間搔弄,已是汗流浹背。 一陣腳步聲響...
    開封第一講書人閱讀 33,139評論 1 272
  • 我被黑心中介騙來泰國打工丰滑, 沒想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留顾犹,地道東北人。 一個月前我還...
    沈念sama閱讀 48,377評論 3 373
  • 正文 我出身青樓褒墨,卻偏偏與公主長得像炫刷,于是被迫代替她去往敵國和親。 傳聞我的和親對象是個殘疾皇子郁妈,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 45,060評論 2 355

推薦閱讀更多精彩內(nèi)容