布局是一種可用于放置很多控件的容器辅斟,它可以按照一定的規(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:
可以通過指定部分控件的 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 即可到忽。
<?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~