Android布局控件之常用linearlayout布局
LinearLayout是線性布局控件,它包含的子控件將以橫向或豎向的方式排列,按照相對位置來排列所有的widgets或者其他的containers,超過邊界時,某些控件將缺失或消失。因此一個垂直列表的每一行只會有一個widget或者是container,而不管他們有多寬,而一個水平列表將會只有一個行高(高度為最高子控件的高度加上邊框高度)。LinearLayout保持其所包含的widget或者是container之間的間隔以及互相對齊(相對一個控件的右對齊、中間對齊或者左對齊)。

xml屬性
android:baselineAligned:是否允許用戶調(diào)整它內(nèi)容的基線。
android:baselineAlignedChildIndex:當(dāng)一個線性布局與另一個布局是按基線對齊的一部分,它可以指定其內(nèi)容的基線對齊方式。
android:gravity:指定如何在該對象中放置此對象的內(nèi)容(x/y坐標(biāo)值)。
android:orientation:設(shè)置它內(nèi)容的對其方向(橫向/豎向)。
gravity 這個英文單詞是重心的意思,在這里就表示??课恢玫囊馑?。
android:layout_gravity 和 android:gravity 的區(qū)別
從名字上可以看到,android:gravity是對元素本身說的,元素本身的文本顯示在什么地方靠著換個屬性設(shè)置,不過不設(shè)置默認是在左側(cè)的。
android:layout_gravity是相對與它的父元素說的,說明元素顯示在父元素的什么位置。
比如說button:android:layout_gravity 表示按鈕在界面上的位置。 android:gravity表示button上的字在button上的位置。
可選值
這兩個屬性可選的值有:top、bottom、left、right、center_vertical、fill_vertical、center_horizontal、fill_horizontal、center、fill、clip_vertical。
而且這些屬性是可以多選的,用“|”分開。
默認這個的值是:Gravity.LEFT
LinearLayout還支持為其包含的widget或者是container指定填充權(quán)值。好處就是允許其包含的widget或者是container可以填充屏幕上的剩余空間。這也避免了在一個大屏幕中,一串widgets或者是containers擠成一堆的情況,而是允許他們放大填充空白。剩余的空間會按這些widgets或者是containers指定的權(quán)值比例分配屏幕。默認的 weight 值為0,表示按照widgets或者是containers實際大小來顯示,若高于0的值,則將Container剩余可用空間分割,分割大小具體取決于每一個widget或者是container的layout_weight及該權(quán)值在所有widgets或者是containers中的比例。例如,如果有三個文本框,其中兩個指定的權(quán)值為1,那么,這兩個文本框?qū)⒌缺壤胤糯螅⑻顫M剩余的空間,而第三個文本框不會放大,按實際大小來顯示。如果前兩個文本框的取值一個為2,一個為1,顯示第三個文本框后剩余的空間的2/3給權(quán)值為2的,1/3大小給權(quán)值為1的。也就是權(quán)值越大,重要度越大。
如果LinearLayout包含子LinearLayout,子LinearLayout之間的權(quán)值越大的,重要度則越小。如果有LinearLayout A包含LinearLayout C,D,C的權(quán)值為2,D的權(quán)值為1,則屏幕的2/3空間分給權(quán)值為1的D,1/3分給權(quán)值為2的C。在LinearLayout嵌套的情況下,子LinearLayout必須要設(shè)置權(quán)值,否則默認的情況是未設(shè)置權(quán)值的子LinearLayout占據(jù)整個屏幕
用linearlayout完成這樣的布局效果,這樣的布局還是比較常用的,具體的xml代碼如下:
1.
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="horizontal" android:baselineAligned="true" android:layout_width="fill_parent" android:layout_height="fill_parent" > <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="按鈕1" /> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="按鈕2" /> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="按鈕3" /> </LinearLayout>
2.
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:baselineAligned="true" android:layout_width="fill_parent" android:layout_height="fill_parent" > <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="按鈕1" /> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="按鈕2" /> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="按鈕3" /> </LinearLayout>
3.
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:baselineAligned="true" android:layout_width="fill_parent" android:layout_height="fill_parent" android:gravity="center_horizontal" > <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="按鈕1" /> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="按鈕2" /> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="按鈕3" /> </LinearLayout>
相關(guān)文章
Android使用自定義View繪制漸隱漸現(xiàn)動畫
這篇文章主要介紹了Android使用自定義View繪制漸隱漸現(xiàn)動畫效果的相關(guān)內(nèi)容,非常不錯,具有參考借鑒價值,需要的朋友可以參考下2016-09-09
Android 實現(xiàn)無網(wǎng)絡(luò)傳輸文件的示例代碼
本篇文章主要介紹了Android 實現(xiàn)無網(wǎng)絡(luò)傳輸文件的示例代碼,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2018-02-02
Android RecyclerView設(shè)置下拉刷新的實現(xiàn)方法
這篇文章主要介紹了Android RecyclerView設(shè)置下拉刷新的實現(xiàn)方法,希望通過本文通過SwipeRefreshLayout方式實現(xiàn)下拉刷新,需要的朋友可以參考下2017-10-10
Android DrawerLayout布局與NavigationView導(dǎo)航菜單應(yīng)用
這篇文章主要介紹了Android DrawerLayout抽屜布局與NavigationView導(dǎo)航菜單應(yīng)用,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)吧2023-01-01

