日韩av黄I国产麻豆传媒I国产91av视频在线观看I日韩一区二区三区在线看I美女国产在线I麻豆视频国产在线观看I成人黄色短片

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 >

Android线性布局(Linear Layout)

發布時間:2025/4/16 60 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Android线性布局(Linear Layout) 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

Android線性布局(Linear Layout)

?

LinearLayout是一個view組(view group),其包含的所有子view都以一個方向排列,垂直或是水平方向。我們能夠用android:orientation屬性來指定布局的方向。


圖1

LinearLayout中所有的子view依次排列,所以垂直列表的每一行只有一個子view,而不管行有多寬。水平列表只有一個行高(行高由最高子view的高度+padding(填充)來決定)。LinearLayout關注子view之間的margins(邊緣)和每個子view的gravity(對齊方式,右、中間或是左對齊)。

?

LinearLayout也支持用android:layout_weight屬性為單個子view指定權重(weight)。這個屬性為一個view指定一個非常重要的值,此值指定了該view需要占用屏幕上多大的空間。一個更大的權重值運行子view擴展到填充滿其父view的剩余空間。子view能夠指定權重值,然后view組中的剩余空間會按照聲明的權重所占的比例來分配。默認的權重是0。

?

比如,如果有文本框(text field),其中兩個聲明權重為1,另一個沒有指定權重(默認值為0)的文本框不會擴展,它只會占據它的內容所需要的區域。在所有這個三個文本框被測量后,其他兩個文本框將平分剩余的空間。如果第3個文本框權重值為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

為了更好去理解權重的意義,我們做一些修改,先來看下面一組權重值的效果:

(1)??To=subject=0(權重),message=2,顯示如下:


圖3

由于to和subject編輯框的屬性android:layout_height="wrap_content"表示它們要求其高度可以包住內容,而且android:layout_weight="0"(或者是不用這個屬性),0表示需要顯示多大的視圖就占據多大的屏幕空間,所以to和subjec編輯框就只占用能包住它們內容的屏幕空間就可以了。那message編輯框的權重只要不是為0,那么它就占用除去to和subject編輯框占用的空間之外的空間。

?

(2)??To=subject=1(權重),message=2,顯示如下:


圖4

雖然to和subject編輯框的屬性android:layout_height="wrap_content",但因為它們的權重值為1(非零),則參與父view可用空間的分割,分割大小具體取決于每一個視圖的layout_weight值以及該值在當前屏幕布局的整體 layout_weight值和在其它視圖屏幕布局的layout_weight值中所占的比率而定,比如這里to和subject的權重值都為1,而message的為2,那么to和subject這兩個view分別占用整個屏幕可用空間的1/4,而message占用2/4。

?

如果需要所有的子view大小一樣,每個view的android:layout_height設置為0dp(對于垂直布局),或是每個view的android:layout_width設置為0dp(對于水平布局) ,然后設置每個view的android:layout_weight為1.

?

?

Android開發者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---理解應用

http://hi.baidu.com/justtmiss/item/a5b59909c688a6e4ff240dac

?

Android UI學習 - Linear Layout,RelativeLayout

http://kb.cnblogs.com/page/73497/

?

總結

以上是生活随笔為你收集整理的Android线性布局(Linear Layout)的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。