說說 Android 的 UI 布局

布局是一種可用于放置很多控件的容器辅斟,它可以按照一定的規(guī)律調(diào)整內(nèi)部控件的位置,從而編寫出漂亮的界面龟糕。當(dāng)然桐磁,布局的內(nèi)部除了放置控件外,也可以放置布局讲岁,通過多層布局的嵌套我擂,我們就能夠?qū)崿F(xiàn)一些比較復(fù)雜的界面咯O(∩_∩)O~

1 線性布局(LinearLayout )

線性布局會將它所包含的所有控件放在線性方向上依次排列。

我們來實踐一下缓艳,布局文件:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    >

    <Button
        android:id="@+id/button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="按鈕 1"
        />

    <Button
        android:id="@+id/button2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="按鈕 2"
        />

    <Button
        android:id="@+id/button3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="按鈕 3"
        />

</LinearLayout>

線性布局

我們在 LinearLayout 中新建了 3 個按鈕校摩,每個按鈕的長和寬都是 wrap_content,并
指定了排列方向是垂直排列l(wèi)阶淘。

修改 LinearLayout 的排列方向為水平排列:

 android:orientation="horizontal"
水平排列

注意衙吩,如果線性布局的排列方向是水平時,那么內(nèi)部控件的寬度就不能是 match_parent溪窒,因為這樣一個單獨控件就會將整個水平方向占滿坤塞,其他的控件就看不見咯。同樣澈蚌,如果線性布局的排列方向是垂直時摹芙,那么內(nèi)部控件的高度也不能是 match_parent。

使用 android:layout_gravity 可以指定控件在布局中的對齊方式宛瞄。注意浮禾,線性布局的排列方向是水平時,只有垂直方向上的對齊方式才有效份汗,因為此時水平方向上的長度是不固定的伐厌,每添加一個控件,水平方向上的長度都會改變裸影,因而無法指定該方向上的對齊方式挣轨。同理,如果線性布局的排列方向是垂直時轩猩,只有在水平方向上的對齊才有效卷扮。

我們?yōu)檫@三個按鈕加上不同的對齊方式:

<Button
    android:id="@+id/button1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="按鈕 1"
    android:layout_gravity="top"
    />

<Button
    android:id="@+id/button2"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="按鈕 2"
    android:layout_gravity="center_vertical"
    />

<Button
    android:id="@+id/button3"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="按鈕 3"
    android:layout_gravity="bottom"
    />
布局中的對齊方式

現(xiàn)在使用 android:layout_weight 屬性,通過比例的方式來指定控件的大小均践,它在手機屏幕的適配性上起到了非常重要的作用晤锹。假設(shè)我們需要編寫一個消息發(fā)送界面,它包含一個文本輸入框和一個發(fā)送按鈕:

<EditText
    android:id="@+id/input_message"
    android:layout_width="0dp"
    android:layout_height="wrap_content"
    android:layout_weight="1"
    android:hint="請輸入消息" />

<Button
    android:id="@+id/send"
    android:layout_width="0dp"
    android:layout_height="wrap_content"
    android:layout_weight="1"
    android:text="發(fā)送" />

這里把 EditText 和 Button 的寬度都指定成了 0dp彤委。dp 會隨著不同屏幕而改變控件長度的像素數(shù)量鞭铆,記住 dp 最終都會轉(zhuǎn)化為像素來衡量大小的哦。

然后在 EditText 和 Button 里都將 android:layout_weight 屬性的值指定為 1,這表示 EditText 和 Button 將在水平方向上各占寬度的 50%车遂,即 1:1封断。

如果把 EditText 與 Button 的 android:layout_weight 屬性分別設(shè)定為 3,2,即表示它們的寬度比重為 3:2:

寬度占比為 3:2

可以通過指定部分控件的 layout_weight 值舶担,來實現(xiàn)更好的效果:

<EditText
    android:id="@+id/input_message"
    android:layout_width="0dp"
    android:layout_height="wrap_content"
    android:layout_weight="1"
    android:hint="請輸入消息" />

<Button
    android:id="@+id/send"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="發(fā)送" />

這里的 Button 的寬度仍然按照 wrap_content 來計算坡疼,EditText 的 :layout_weight 設(shè)置為 1,這樣它就會占滿屏幕所有的剩余空間衣陶。使用這種方式編寫的界面柄瑰,不僅能夠適配各種屏幕,而且看起來也很舒服剪况。

更好的效果

2 相對布局(RelativeLayout )

相對布局顯得更加隨意一些教沾,它可以通過相對定位的方式讓控件出現(xiàn)在布局的任意位置。

<?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/button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true"
        android:text="按鈕 1" />

    <Button
        android:id="@+id/button2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentRight="true"
        android:layout_alignParentTop="true"
        android:text="按鈕 2" />

    <Button
        android:id="@+id/button3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true"
        android:text="按鈕 3" />

    <Button
        android:id="@+id/button4"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_alignParentLeft="true"
        android:text="按鈕 4" />

    <Button
        android:id="@+id/button5"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_alignParentRight="true"
        android:text="按鈕 5" />

</RelativeLayout>

相對布局示例

這里我們讓 Button 1 相對父布局的左上角對齊译断,Button 2 相對父布局的右上角對齊详囤,Button 3居中顯示,Button 4 相對父布局的左下角對齊镐作,Button 5 相對父布局的右下角對齊藏姐。

控件也可以相對于其他控件進行定位的哦:

<Button
    android:id="@+id/button3"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_centerInParent="true"
    android:text="按鈕 3" />

<Button
    android:id="@+id/button1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_above="@+id/button3"
    android:layout_toLeftOf="@id/button3"
    android:text="按鈕 1" />

<Button
    android:id="@+id/button2"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_above="@+id/button3"
    android:layout_toRightOf="@id/button3"
    android:text="按鈕 2" />


<Button
    android:id="@+id/button4"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_below="@+id/button3"
    android:layout_toLeftOf="@id/button3"
    android:text="按鈕 4" />

<Button
    android:id="@+id/button5"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_below="@+id/button3"
    android:layout_toRightOf="@id/button3"
    android:text="按鈕 5" />
相對于其他控件進行定位

這里,我們讓其它按鈕都相對于 “按鈕 3” 的位置進行定位该贾。

屬性 說明
android:layout_above 讓一個控件位于另一個控件的上方羔杨。
android:layout_below 讓一個控件位于另一個控件的下方。
android:layout_toLeftOf 讓一個控件位于另一個控件的左邊杨蛋。
android:layout_toRightOf 讓一個控件位于另一個控件的右邊兜材。
android:layout_alignLeft 一個控件的左邊緣和另一個控件的左邊緣對齊。
android:layout_alignRight 讓一個控件的右邊緣和另一個控件的右邊緣對齊逞力。
android:layout_alignTop 讓一個控件的上邊緣和另一個控件的上邊緣對齊曙寡。
android:layout_alignBottom 讓一個控件的下邊緣和另一個控件的下邊緣對齊。

以上這些屬性都需要指定相對控件 id 的引用寇荧。

注意举庶,當(dāng)一個控件去引用另一個控件的 id 時,這個控件一定要定義在引用控件的后面揩抡,不然會出現(xiàn)找不到 id 的情況户侥。

3 幀布局(FrameLayout )

幀布局中的所有控件都會擺放在布局的左上角。

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    >

    <TextView
        android:id="@+id/text"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="這是一段文本"
        />

    <ImageView
        android:id="@+id/image"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@mipmap/ic_launcher"
        />

</FrameLayout>

幀布局

可以看出峦嗤,按鈕和圖片都是位于布局的左上角蕊唐。由于圖片是在按鈕之后添加的,因此圖
片壓在了按鈕的上面烁设。

可以使用 layout_gravity 來指定控件在布局中的對齊方式:

<TextView
    android:id="@+id/text"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="這是一段文本"
    android:layout_gravity="left"
    />

<ImageView
    android:id="@+id/image"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:src="@mipmap/ic_launcher"
    android:layout_gravity="right"
    />
指定控件的對齊方式

因為幀布局缺少定位方式替梨,所以使用它的應(yīng)用場景較少哦O(∩_∩)O~

4 百分比布局

在百分比布局中,我們可以直接指定控件在布局中所占的百分比。

為了讓所有的 Android 版本都能用上這個布局副瀑,Android 團隊把它定義在 support 庫中弓熏。所以我們只要在 build.gradle 中添加百分比布局依賴就可以使用它啦O(∩_∩)O~

打開 app/build.gradle 文件,在 dependencies 中添加:

   compile 'com.android.support:percent:24.2.1'

每當(dāng)修改了 build.gradle 文件之后俗扇,Android studio 都要彈出提示硝烂,讓我們更新 (Sync Now)箕别,記得點擊更新哦O(∩_∩)O~

如果拋出 Failed to resolve: compile 'xxx'铜幽,請先檢查拼寫是否正確;如果還是不行串稀,請到C:\Users\Administrator\AppData\Local\Android\sdk\extras\android\m2repository\com\android\support\percent 路徑下除抛,找到一個存在的 24.x.x 版本,更新文件母截,并點擊 Sync Now 即可到忽。

gradle 資源庫中的 percent 版本
<?xml version="1.0" encoding="utf-8"?>
<android.support.percent.PercentFrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <Button
        android:id="@+id/button1"
        android:layout_gravity="left|top"
        android:text="按鈕 1"
        app:layout_heightPercent="50%"
        app:layout_widthPercent="50%"></Button>

    <Button
        android:id="@+id/button2"
        android:layout_gravity="right|top"
        android:text="按鈕 2"
        app:layout_heightPercent="50%"
        app:layout_widthPercent="50%"></Button>

    <Button
    android:id="@+id/button3"
    android:layout_gravity="left|bottom"
    android:text="按鈕 3"
    app:layout_heightPercent="50%"
    app:layout_widthPercent="50%"></Button>

    <Button
        android:id="@+id/button4"
        android:layout_gravity="right|bottom"
        android:text="按鈕 4"
        app:layout_heightPercent="50%"
        app:layout_widthPercent="50%"></Button>


</android.support.percent.PercentFrameLayout>

因為百分比布局不是內(nèi)置于 Android SDK 中,所以我們這里引用的是完整的包路徑清寇,然后還定義了 app 命名空間喘漏,用于引用百分比布局的屬性。

由于百分比布局繼承自幀布局华烟,所以所有的控件都是默認(rèn)擺放在布局的左上角翩迈,這里我們借助 于 layout_gravity 把四個按鈕分別放置于左上、左下盔夜、右上负饲、右下位置:

百分比布局示例

另外一個 PercentRelativeLayout 繼承自 RelativeLayout,它可以使用 app:layout_widthPercent 和 app:layout_heightPercent 的百分比屬性來設(shè)置控件的寬度與高度哦O(∩_∩)O~


除了以上這些布局喂链,Android 中還有 AbsoluteLayout 與 TableLayout 布局返十,但因為實踐中用的實在是太少,所以這里就不再累述咯O(∩_∩)O~

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末椭微,一起剝皮案震驚了整個濱河市洞坑,隨后出現(xiàn)的幾起案子,更是在濱河造成了極大的恐慌蝇率,老刑警劉巖检诗,帶你破解...
    沈念sama閱讀 217,657評論 6 505
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件,死亡現(xiàn)場離奇詭異瓢剿,居然都是意外死亡逢慌,警方通過查閱死者的電腦和手機,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 92,889評論 3 394
  • 文/潘曉璐 我一進店門间狂,熙熙樓的掌柜王于貴愁眉苦臉地迎上來攻泼,“玉大人,你說我怎么就攤上這事∶Σぃ” “怎么了何鸡?”我有些...
    開封第一講書人閱讀 164,057評論 0 354
  • 文/不壞的土叔 我叫張陵,是天一觀的道長牛欢。 經(jīng)常有香客問我骡男,道長,這世上最難降的妖魔是什么傍睹? 我笑而不...
    開封第一講書人閱讀 58,509評論 1 293
  • 正文 為了忘掉前任隔盛,我火速辦了婚禮,結(jié)果婚禮上拾稳,老公的妹妹穿的比我還像新娘吮炕。我一直安慰自己,他們只是感情好访得,可當(dāng)我...
    茶點故事閱讀 67,562評論 6 392
  • 文/花漫 我一把揭開白布龙亲。 她就那樣靜靜地躺著,像睡著了一般悍抑。 火紅的嫁衣襯著肌膚如雪鳄炉。 梳的紋絲不亂的頭發(fā)上,一...
    開封第一講書人閱讀 51,443評論 1 302
  • 那天搜骡,我揣著相機與錄音拂盯,去河邊找鬼。 笑死浆兰,一個胖子當(dāng)著我的面吹牛磕仅,可吹牛的內(nèi)容都是我干的。 我是一名探鬼主播簸呈,決...
    沈念sama閱讀 40,251評論 3 418
  • 文/蒼蘭香墨 我猛地睜開眼榕订,長吁一口氣:“原來是場噩夢啊……” “哼!你這毒婦竟也來了蜕便?” 一聲冷哼從身側(cè)響起劫恒,我...
    開封第一講書人閱讀 39,129評論 0 276
  • 序言:老撾萬榮一對情侶失蹤,失蹤者是張志新(化名)和其女友劉穎轿腺,沒想到半個月后两嘴,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體,經(jīng)...
    沈念sama閱讀 45,561評論 1 314
  • 正文 獨居荒郊野嶺守林人離奇死亡族壳,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點故事閱讀 37,779評論 3 335
  • 正文 我和宋清朗相戀三年憔辫,在試婚紗的時候發(fā)現(xiàn)自己被綠了。 大學(xué)時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片仿荆。...
    茶點故事閱讀 39,902評論 1 348
  • 序言:一個原本活蹦亂跳的男人離奇死亡贰您,死狀恐怖坏平,靈堂內(nèi)的尸體忽然破棺而出,到底是詐尸還是另有隱情锦亦,我是刑警寧澤舶替,帶...
    沈念sama閱讀 35,621評論 5 345
  • 正文 年R本政府宣布,位于F島的核電站杠园,受9級特大地震影響顾瞪,放射性物質(zhì)發(fā)生泄漏。R本人自食惡果不足惜抛蚁,卻給世界環(huán)境...
    茶點故事閱讀 41,220評論 3 328
  • 文/蒙蒙 一陈醒、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧篮绿,春花似錦孵延、人聲如沸吕漂。這莊子的主人今日做“春日...
    開封第一講書人閱讀 31,838評論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽惶凝。三九已至吼虎,卻和暖如春,著一層夾襖步出監(jiān)牢的瞬間苍鲜,已是汗流浹背思灰。 一陣腳步聲響...
    開封第一講書人閱讀 32,971評論 1 269
  • 我被黑心中介騙來泰國打工, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留混滔,地道東北人洒疚。 一個月前我還...
    沈念sama閱讀 48,025評論 2 370
  • 正文 我出身青樓,卻偏偏與公主長得像坯屿,于是被迫代替她去往敵國和親油湖。 傳聞我的和親對象是個殘疾皇子,可洞房花燭夜當(dāng)晚...
    茶點故事閱讀 44,843評論 2 354

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