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 這個英文單詞是重心的意思,在這里就表示??课恢玫囊馑肌?/p>
android:layout_gravity 和 android:gravity 的區(qū)別
從名字上可以看到,android:gravity是對元素本身說的,元素本身的文本顯示在什么地方靠著換個屬性設(shè)置,不過不設(shè)置默認(rèn)是在左側(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。
而且這些屬性是可以多選的,用“|”分開。
默認(rèn)這個的值是:Gravity.LEFT
LinearLayout還支持為其包含的widget或者是container指定填充權(quán)值。好處就是允許其包含的widget或者是container可以填充屏幕上的剩余空間。這也避免了在一個大屏幕中,一串widgets或者是containers擠成一堆的情況,而是允許他們放大填充空白。剩余的空間會按這些widgets或者是containers指定的權(quán)值比例分配屏幕。默認(rèn)的 weight 值為0,表示按照widgets或者是containers實際大小來顯示,若高于0的值,則將Container剩余可用空間分割,分割大小具體取決于每一個widget或者是container的layout_weight及該權(quán)值在所有widgets或者是containers中的比例。例如,如果有三個文本框,其中兩個指定的權(quán)值為1,那么,這兩個文本框?qū)⒌缺壤胤糯?,并填滿剩余的空間,而第三個文本框不會放大,按實際大小來顯示。如果前兩個文本框的取值一個為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)值,否則默認(rè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編程實現(xiàn)應(yīng)用程序開機(jī)自啟動的方法
這篇文章主要介紹了Android編程實現(xiàn)應(yīng)用程序開機(jī)自啟動的方法,涉及Android權(quán)限控制及廣播操作相關(guān)技巧,需要的朋友可以參考下2017-02-02Android design包自定義tablayout的底部導(dǎo)航欄的實現(xiàn)方法
這篇文章主要介紹了Android design包自定義tablayout的底部導(dǎo)航欄的實現(xiàn)方法,非常不錯,具有參考借鑒價值,需要的朋友參考下2017-01-01android開發(fā)中ListView與Adapter使用要點介紹
項目用到ListView,由于要用到 ImageView ,圖片源不是在資源里面的,沒法使用資源 ID,因此無法直接使用SimpleAdapter,要自己寫一個Adapter。 在使用ListView和Adapter需要注意以下幾點2013-06-06RecyclerView實現(xiàn)抖音縱向滾動ViewPager效果
這篇文章主要為大家詳細(xì)介紹了RecyclerView實現(xiàn)抖音縱向滾動ViewPager效果,具有一定的參考價值,感興趣的小伙伴們可以參考一下2018-07-07Android實現(xiàn)SQLite添加、更新及刪除行的方法
這篇文章主要介紹了Android實現(xiàn)SQLite添加、更新及刪除行的方法,涉及Android基于SQLiteDatabase類操作SQLite數(shù)據(jù)庫的基本技巧,需要的朋友可以參考下2016-08-08