Android布局控件之常用linearlayout布局
LinearLayout是線性布局控件,它包含的子控件將以橫向或豎向的方式排列,按照相對(duì)位置來(lái)排列所有的widgets或者其他的containers,超過(guò)邊界時(shí),某些控件將缺失或消失。因此一個(gè)垂直列表的每一行只會(huì)有一個(gè)widget或者是container,而不管他們有多寬,而一個(gè)水平列表將會(huì)只有一個(gè)行高(高度為最高子控件的高度加上邊框高度)。LinearLayout保持其所包含的widget或者是container之間的間隔以及互相對(duì)齊(相對(duì)一個(gè)控件的右對(duì)齊、中間對(duì)齊或者左對(duì)齊)。
xml屬性
android:baselineAligned:是否允許用戶調(diào)整它內(nèi)容的基線。
android:baselineAlignedChildIndex:當(dāng)一個(gè)線性布局與另一個(gè)布局是按基線對(duì)齊的一部分,它可以指定其內(nèi)容的基線對(duì)齊方式。
android:gravity:指定如何在該對(duì)象中放置此對(duì)象的內(nèi)容(x/y坐標(biāo)值)。
android:orientation:設(shè)置它內(nèi)容的對(duì)其方向(橫向/豎向)。
gravity 這個(gè)英文單詞是重心的意思,在這里就表示??课恢玫囊馑?。
android:layout_gravity 和 android:gravity 的區(qū)別
從名字上可以看到,android:gravity是對(duì)元素本身說(shuō)的,元素本身的文本顯示在什么地方靠著換個(gè)屬性設(shè)置,不過(guò)不設(shè)置默認(rèn)是在左側(cè)的。
android:layout_gravity是相對(duì)與它的父元素說(shuō)的,說(shuō)明元素顯示在父元素的什么位置。
比如說(shuō)button:android:layout_gravity 表示按鈕在界面上的位置。 android:gravity表示button上的字在button上的位置。
可選值
這兩個(gè)屬性可選的值有:top、bottom、left、right、center_vertical、fill_vertical、center_horizontal、fill_horizontal、center、fill、clip_vertical。
而且這些屬性是可以多選的,用“|”分開(kāi)。
默認(rèn)這個(gè)的值是:Gravity.LEFT
LinearLayout還支持為其包含的widget或者是container指定填充權(quán)值。好處就是允許其包含的widget或者是container可以填充屏幕上的剩余空間。這也避免了在一個(gè)大屏幕中,一串widgets或者是containers擠成一堆的情況,而是允許他們放大填充空白。剩余的空間會(huì)按這些widgets或者是containers指定的權(quán)值比例分配屏幕。默認(rèn)的 weight 值為0,表示按照widgets或者是containers實(shí)際大小來(lái)顯示,若高于0的值,則將Container剩余可用空間分割,分割大小具體取決于每一個(gè)widget或者是container的layout_weight及該權(quán)值在所有widgets或者是containers中的比例。例如,如果有三個(gè)文本框,其中兩個(gè)指定的權(quán)值為1,那么,這兩個(gè)文本框?qū)⒌缺壤胤糯?,并填滿剩余的空間,而第三個(gè)文本框不會(huì)放大,按實(shí)際大小來(lái)顯示。如果前兩個(gè)文本框的取值一個(gè)為2,一個(gè)為1,顯示第三個(gè)文本框后剩余的空間的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ù)整個(gè)屏幕
用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編程實(shí)現(xiàn)應(yīng)用程序開(kāi)機(jī)自啟動(dòng)的方法
這篇文章主要介紹了Android編程實(shí)現(xiàn)應(yīng)用程序開(kāi)機(jī)自啟動(dòng)的方法,涉及Android權(quán)限控制及廣播操作相關(guān)技巧,需要的朋友可以參考下2017-02-02Android design包自定義tablayout的底部導(dǎo)航欄的實(shí)現(xiàn)方法
這篇文章主要介紹了Android design包自定義tablayout的底部導(dǎo)航欄的實(shí)現(xiàn)方法,非常不錯(cuò),具有參考借鑒價(jià)值,需要的朋友參考下2017-01-01Android Eclipse 注釋模板的使用(圖文說(shuō)明)
為提高代碼的可讀性以及后期的可維護(hù)性,為我們的代碼加上規(guī)范化的注釋是很有必要,不僅有利于提高自己的專業(yè)素養(yǎng),也能方便他人2013-12-12android開(kāi)發(fā)中ListView與Adapter使用要點(diǎn)介紹
項(xiàng)目用到ListView,由于要用到 ImageView ,圖片源不是在資源里面的,沒(méi)法使用資源 ID,因此無(wú)法直接使用SimpleAdapter,要自己寫(xiě)一個(gè)Adapter。 在使用ListView和Adapter需要注意以下幾點(diǎn)2013-06-06RecyclerView實(shí)現(xiàn)抖音縱向滾動(dòng)ViewPager效果
這篇文章主要為大家詳細(xì)介紹了RecyclerView實(shí)現(xiàn)抖音縱向滾動(dòng)ViewPager效果,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-07-07Android實(shí)現(xiàn)SQLite添加、更新及刪除行的方法
這篇文章主要介紹了Android實(shí)現(xiàn)SQLite添加、更新及刪除行的方法,涉及Android基于SQLiteDatabase類操作SQLite數(shù)據(jù)庫(kù)的基本技巧,需要的朋友可以參考下2016-08-08Android基礎(chǔ)知識(shí)之單點(diǎn)觸摸
這篇文章主要為大家詳細(xì)介紹了Android基礎(chǔ)知識(shí)之單點(diǎn)觸摸,很簡(jiǎn)單的操作,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2016-06-06