Android线性布局(Linear Layout)
Android線性布局(Linear Layout)
?
LinearLayout是一個view組(view group),其包含的所有子view都以一個方向排列,垂直或是水平方向。我們能夠用android:orientation屬性來指定布局的方向。
圖1
LinearLayout中所有的子view依次排列,所以垂直列表的每一行只有一個子view,而不管行有多寬。水平列表只有一個行高(行高由最高子view的高度+padding(填充)來決定)。LinearLayout關(guān)注子view之間的margins(邊緣)和每個子view的gravity(對齊方式,右、中間或是左對齊)。
?
LinearLayout也支持用android:layout_weight屬性為單個子view指定權(quán)重(weight)。這個屬性為一個view指定一個非常重要的值,此值指定了該view需要占用屏幕上多大的空間。一個更大的權(quán)重值運行子view擴(kuò)展到填充滿其父view的剩余空間。子view能夠指定權(quán)重值,然后view組中的剩余空間會按照聲明的權(quán)重所占的比例來分配。默認(rèn)的權(quán)重是0。
?
比如,如果有文本框(text field),其中兩個聲明權(quán)重為1,另一個沒有指定權(quán)重(默認(rèn)值為0)的文本框不會擴(kuò)展,它只會占據(jù)它的內(nèi)容所需要的區(qū)域。在所有這個三個文本框被測量后,其他兩個文本框?qū)⑵椒质S嗟目臻g。如果第3個文本框權(quán)重值為2,它就申明了自己比其他的文本框更重要,它占用了剩余空間的一半,另一半由那兩個文本框平分。示例代碼如下:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
? ? android:layout_width="fill_parent"
? ? android:layout_height="fill_parent"
? ? android:paddingLeft="16dp"
? ? android:paddingRight="16dp"
? ? android:orientation="vertical">
? ? <EditText
? ? ? ? android:layout_width="fill_parent"
? ? ? ? android:layout_height="wrap_content"
? ? ? ? android:hint="@string/to"/>
? ? <EditText
? ? ? ? android:layout_width="fill_parent"
? ? ? ? android:layout_height="wrap_content"
? ? ? ? android:hint="@string/subject"/>
? ? <EditText
? ? ? ? android:layout_width="fill_parent"
? ? ? ? android:layout_height="0dp"
? ? ? ? android:layout_weight="1"
? ? ? ? android:gravity="top"
? ? ? ? android:hint="@string/message"/>
? ? <Button
? ? ? ? android:layout_width="100dp"
? ? ? ? android:layout_height="wrap_content"
? ? ? ? android:layout_gravity="right"
? ? ? ? android:text="@string/send"/>
</LinearLayout>
還需要在LinearLayout\res\values\strings.XML文件中增加這些字符串的定義
<stringname="to">To</string>
<stringname="subject">Subject</string>
<stringname="message">Message</string>
<stringname="send">Send</string>
Activity中布局的效果如下圖:
圖2
為了更好去理解權(quán)重的意義,我們做一些修改,先來看下面一組權(quán)重值的效果:
(1)??To=subject=0(權(quán)重),message=2,顯示如下:
圖3
由于to和subject編輯框的屬性android:layout_height="wrap_content"表示它們要求其高度可以包住內(nèi)容,而且android:layout_weight="0"(或者是不用這個屬性),0表示需要顯示多大的視圖就占據(jù)多大的屏幕空間,所以to和subjec編輯框就只占用能包住它們內(nèi)容的屏幕空間就可以了。那message編輯框的權(quán)重只要不是為0,那么它就占用除去to和subject編輯框占用的空間之外的空間。
?
(2)??To=subject=1(權(quán)重),message=2,顯示如下:
圖4
雖然to和subject編輯框的屬性android:layout_height="wrap_content",但因為它們的權(quán)重值為1(非零),則參與父view可用空間的分割,分割大小具體取決于每一個視圖的layout_weight值以及該值在當(dāng)前屏幕布局的整體 layout_weight值和在其它視圖屏幕布局的layout_weight值中所占的比率而定,比如這里to和subject的權(quán)重值都為1,而message的為2,那么to和subject這兩個view分別占用整個屏幕可用空間的1/4,而message占用2/4。
?
如果需要所有的子view大小一樣,每個view的android:layout_height設(shè)置為0dp(對于垂直布局),或是每個view的android:layout_width設(shè)置為0dp(對于水平布局) ,然后設(shè)置每個view的android:layout_weight為1.
?
?
Android開發(fā)者Linear Layouts
http://developer.android.com/guide/topics/ui/layout/linear.html
?
Android布局---線性布局(Linear Layout)---別人翻譯
http://www.2cto.com/kf/201301/183527.html
?
線性布局(Linear Layout)---理解應(yīng)用
http://hi.baidu.com/justtmiss/item/a5b59909c688a6e4ff240dac
?
Android UI學(xué)習(xí) - Linear Layout,RelativeLayout
http://kb.cnblogs.com/page/73497/
?
總結(jié)
以上是生活随笔為你收集整理的Android线性布局(Linear Layout)的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Android XML使用的学习记录
- 下一篇: Android关于AndroidMani