Android_Layout (一)
layout (布局) ?--->Android 有五大布局,分別是:
- LinearLayout :?線性布局,子組件按照垂直或者水平方向來布局。
- RelativeLayout :相對布局,按照控件之間的相互位置進(jìn)行參照物的概念排布,存在一個排布,存在一個參照物的概念, 一般來說在相對布局中的控件都會存在id 的屬性。
- TableLayout :表格布局,繼承于線性布局。(用法類似)
- AbsoluteLayout?:絕對布局,是通過指定控件的x/y坐標(biāo)來定位的(顯示效果死板,不利于開發(fā),幾乎被棄用)。
- FrameLayout?:幀布局,畫面顯示的效果
一、線性布局(LiearLayout)
? ?線性布局在開發(fā)中使用最多,具有垂直方向與水平方向的布局方式
通過設(shè)置屬性“android:orientation”控制方向,屬性值垂直(vertical)和水平(horizontal),默認(rèn)水平方向。
常用屬性:
android:layout_gravity 本元素相對于父元素的重力方向
android:gravity 本元素所有子元素的重力方向
android:orientation 線性布局以列或行來顯示內(nèi)部子元素
android:layout_weight 子元素對未占用空間水平或垂直分配權(quán)重值
當(dāng) android:orientation="vertical" 時, 只有水平方向的設(shè)置才起作用,垂直方向的設(shè)置不起作用。即:left,right,center_horizontal 是生效的。!!!!
當(dāng) android:orientation="horizontal" 時, 只有垂直方向的設(shè)置才起作用,水平方向的設(shè)置不起作用。即:top,bottom,center_vertical 是生效的。!!!!
android:layout_gravity 和 android:gravity 的區(qū)別
android:gravity?對元素本身起作用-本身元素顯示在什么位置
android:layout_gravity?相對與它的父元素-元素顯示在父元素的什么位置。
如:Button控件
android:layout_gravity 表示button在界面上的位置
android:gravity表示button上的字在button上的位置。
可選值[多選時用“|”分開]
top、bottom、left、right、center_vertical、fill_vertical、center_horizontal、fill_horizontal、center、fill、clip_vertical。
- top 將對象放在其容器的頂部,不改變其大小.
- bottom 將對象放在其容器的底部,不改變其大小.
- left將對象放在其容器的左側(cè),不改變其大小.
- right將對象放在其容器的右側(cè),不改變其大小.
- center_vertical 將對象縱向居中,不改變其大小.
垂直對齊方式:垂直方向上居中對齊。
fill_vertical 必要的時候增加對象的縱向大小,以完全充滿其容器. 垂直方向填充
center_horizontal 將對象橫向居中,不改變其大小水平對齊方式:水平方向上居中對齊
fill_horizontal 必要的時候增加對象的橫向大小,以完全充滿其容器. 水平方向填充
center 將對象橫縱居中,不改變其大小.
fill 必要的時候增加對象的橫縱向大小,以完全充滿其容器.
clip_vertical 附加選項(xiàng),用于按照容器的邊來剪切對象的頂部和/或底部的內(nèi)容. 剪切基于其縱向?qū)R設(shè)置:頂部對齊時,剪切底部;底部對齊時剪切頂部;除此之外剪切頂部和底部.垂直方向裁剪
clip_horizontal 附加選項(xiàng),用于按照容器的邊來剪切對象的左側(cè)和/或右側(cè)的內(nèi)容. 剪切基于其橫向?qū)R設(shè)置:左側(cè)對齊時,剪切右側(cè);右側(cè)對齊時剪切左側(cè);除此之外剪切左 ? ? ?側(cè)和右側(cè).水平方向裁剪
例子
TextView要讓文本垂直/水平居中顯示,有兩種情況需要考慮:
1、layout_width/layout_height為wrap_content,此時要讓TextView在父控件上居中顯示,必須設(shè)置layout_gravity=”center”。
2、layout_width/layout_height為fill_parent,此時由于TextView已占據(jù)父窗體所有空間,必須設(shè)置gravity=”center”。
二、相對布局
相對布局的子控件會根據(jù)它們所設(shè)置的參照控件和參數(shù)進(jìn)行相對布局。參照控件可以是父控件,也可以是其它子控件,但是被參照的控件必須要在參照它的控件之前定義
常用屬性:
RelativeLayout用到的一些重要的屬性:
第一類:屬性值為true或false
android:layout_centerHrizontal 水平居中
android:layout_centerVertical 垂直居中
android:layout_centerInparent 相對于父元素完全居中
android:layout_alignParentBottom 貼緊父元素的下邊緣
android:layout_alignParentLeft 貼緊父元素的左邊緣
android:layout_alignParentRight 貼緊父元素的右邊緣
android:layout_alignParentTop 貼緊父元素的上邊緣
android:layout_alignWithParentIfMissing 如果對應(yīng)的兄弟元素找不到的話就以父元素做參照物
第二類:屬性值必須為id的引用名“@id/id-name”
android:layout_below 在某元素的下方
android:layout_above 在某元素的的上方
android:layout_toLeftOf 在某元素的左邊
android:layout_toRightOf 在某元素的右邊
android:layout_alignTop 本元素的上邊緣和某元素的的上邊緣對齊
android:layout_alignLeft 本元素的左邊緣和某元素的的左邊緣對齊
android:layout_alignBottom 本元素的下邊緣和某元素的的下邊緣對齊
android:layout_alignRight 本元素的右邊緣和某元素的的右邊緣對齊
第三類:屬性值為具體的像素值,如30dip,40px
android:layout_marginBottom 離某元素底邊緣的距離
android:layout_marginLeft 離某元素左邊緣的距離
android:layout_marginRight 離某元素右邊緣的距離
android:layout_marginTop 離某元素上邊緣的距離
EditText的android:hint
設(shè)置EditText為空時輸入框內(nèi)的提示信息。
android:gravity
android:gravity屬性是對該view 內(nèi)容的限定.比如一個button 上面的text. 你可以設(shè)置該text 在view的靠左,靠右等位置.以button為例,android:gravity="right"則 ? ? ? ? ? ? ? ? ? ? ? ?button上面的文字靠右
android:layout_gravity
android:layout_gravity是用來設(shè)置該view相對與起父view 的位置.比如一個button 在linearlayout里,你想把該button放在靠左、靠右等位置就可以通過該屬性設(shè)置. ? ? ? ? ? ? ? ? ? ? ? ?以button為例,android:layout_gravity="right"則button靠右
android:layout_alignParentRight
使當(dāng)前控件的右端和父控件的右端對齊。這里屬性值只能為true或false,默認(rèn)false。
android:scaleType:
android:scaleType是控制圖片如何resized/moved來匹對ImageView的size。ImageView.ScaleType / android:scaleType值的意義區(qū)別:
CENTER /center 按圖片的原來size居中顯示,當(dāng)圖片長/寬超過View的長/寬,則截取圖片的居中部分顯示
CENTER_CROP / centerCrop 按比例擴(kuò)大圖片的size居中顯示,使得圖片長(寬)等于或大于View的長(寬)
CENTER_INSIDE / centerInside 將圖片的內(nèi)容完整居中顯示,通過按比例縮小或原來的size使得圖片長/寬等于或小于View的長/寬
FIT_CENTER / fitCenter 把圖片按比例擴(kuò)大/縮小到View的寬度,居中顯示
FIT_END / fitEnd 把圖片按比例擴(kuò)大/縮小到View的寬度,顯示在View的下部分位置
FIT_START / fitStart 把圖片按比例擴(kuò)大/縮小到View的寬度,顯示在View的上部分位置
FIT_XY / fitXY 把圖片 不按比例擴(kuò)大/縮小到View的大小顯示
MATRIX / matrix 用矩陣來繪制,動態(tài)縮小放大圖片來顯示。
** 要注意一點(diǎn),Drawable文件夾里面的圖片命名是不能大寫的。
例子:
1 <?xml version="1.0" encoding="utf-8"?>2 <?xml version="1.0" encoding="utf-8"?>3 <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"4 android:layout_width="fill_parent"5 android:layout_height="fill_parent"6 >7 <AnalogClock8 android:id="@+id/aclock"9 android:layout_width="wrap_content" 10 android:layout_height="wrap_content" 11 android:layout_centerInParent="true" /> 12 <DigitalClock 13 android:id="@+id/dclock" 14 android:layout_width="wrap_content" 15 android:layout_height="wrap_content" 16 android:layout_below="@id/aclock" 17 android:layout_alignLeft="@id/aclock" 18 android:layout_marginLeft="40px" /> 19 <TextView 20 android:layout_width="wrap_content" 21 android:layout_height="wrap_content" 22 android:text="當(dāng)前時間:" 23 android:layout_toLeftOf="@id/dclock" 24 android:layout_alignTop="@id/aclock"/> 25 </RelativeLayout>?
三、幀布局(FrameLayout)
幀布局,又叫框架布局,是五大布局中最簡單的一個布局,在這個布局中,整個界面被當(dāng)成一塊空白備用區(qū)域,所有的子元素都不能被指定放置的位置,它們統(tǒng)統(tǒng)放于這塊區(qū)域的左上角,并且后面的子元素直接覆蓋在前面的子元素之上,將前面的子元素部分和全部遮擋。顯示效果如下,第一個TextView被第二個TextView完全遮擋,第三個TextView遮擋了第二個TextView的部分位置
?
常用屬性:
android:foreground:設(shè)置改幀布局容器的前景圖像
android:foregroundGravity:設(shè)置前景圖像顯示的位置
?
例子:
1 <?xml version="1.0" encoding="utf-8"?>2 <FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"3 android:layout_width="fill_parent" android:layout_height="fill_parent">4 <LinearLayout android:id="@+id/linearLayout1"5 android:layout_height="match_parent"6 android:layout_width="match_parent">7 <Button android:text="Button"8 android:id="@+id/button1"9 android:layout_width="wrap_content" 10 android:layout_height="wrap_content"></Button> 11 </LinearLayout> 12 <LinearLayout android:layout_width="match_parent" 13 android:id="@+id/linearLayout3" 14 android:layout_height="match_parent" 15 android:gravity="bottom|right"> 16 <Button android:text="Button" 17 android:id="@+id/button3" 18 android:layout_width="wrap_content" 19 android:layout_height="wrap_content"></Button> 20 </LinearLayout> 21 <LinearLayout android:layout_height="match_parent" 22 android:id="@+id/linearLayout2" 23 android:layout_width="match_parent" 24 android:gravity="right"> 25 <Button android:text="Button" 26 android:id="@+id/button2" 27 android:layout_width="wrap_content" 28 android:layout_height="wrap_content"></Button> 29 </LinearLayout> 30 <LinearLayout android:layout_width="match_parent" 31 android:id="@+id/LinearLayout01" 32 android:layout_height="match_parent" 33 android:gravity="bottom|left"> 34 <Button android:id="@+id/Button01" 35 android:text="Button" 36 android:layout_width="wrap_content" 37 android:layout_height="wrap_content"></Button> 38 </LinearLayout> 39 </FrameLayout>
?
?
四、絕對布局(AbsoluteLayout)
絕對布局的子控件需要指定相對于此坐標(biāo)布局的橫縱坐標(biāo)值,否則將會像框架布局那樣被排在左上角。手機(jī)應(yīng)用需要適應(yīng)不同的屏幕大小,而這種布局模型不能自適應(yīng)屏幕尺寸大小,所以應(yīng)用的相對較少。
?
常用屬性:
1.控制大小
android:layout_width:組件高度
android:layout_height:組件高度
2.控制位置
android:layout_x:設(shè)置組件的X坐標(biāo)
android:layout_y:設(shè)置組件的Y坐標(biāo)
?由于該布局應(yīng)用低,不同的手機(jī),屏幕大小不同,顯示的布局也不一樣,所以我就部深入了解了
五、表格布局(TableLayout)
表格布局模型以行列的形式管理子控件,每一行為一個TableRow的對象,當(dāng)然也可以是一個View的對象。TableRow可以添加子控件,每添加一個為一列。
?
常用屬性:
android:collapseColumns:將TableLayout里面指定的列隱藏,若有多列需要隱藏,請用逗號將需要隱藏的列序號隔開。?????????????
android:stretchColumns:設(shè)置指定的列為可伸展的列,以填滿剩下的多余空白空間,若有多列需要設(shè)置為可伸展,請用逗號將需要伸展的列序號隔開。????????????????
android:shrinkColumns:設(shè)置指定的列為可收縮的列。當(dāng)可收縮的列太寬(內(nèi)容過多)不會被擠出屏幕。當(dāng)需要設(shè)置多列為可收縮時,將列序號用逗號隔開。
? 列元素(Button)屬性:(奇怪的是button 里面沒有android:layout_column 和android:layout_span兩個屬性,寫進(jìn)去無反應(yīng),還不知道為什么)
android:layout_colum:設(shè)置該控件在TableRow中指定的列。
android:layout_span:設(shè)置該控件所跨越的列數(shù)。
?
例子:
?
1 <?xml version="1.0" encoding="utf-8"?>2 <TableLayout xmlns:android="http://schemas.android.com/apk/res/android"3 android:layout_width="fill_parent"4 android:layout_height="fill_parent"5 android:stretchColumns="1">6 <TableRow>7 <TextView8 android:layout_column="1"9 android:padding="3dip" android:text="Row1"/> 10 <TextView 11 android:text="1" 12 android:gravity="right" 13 android:padding="3dip" /> 14 </TableRow> 15 <View 16 android:layout_height="2dip" 17 android:background="#FF909090" /> 18 <TableRow> 19 <TextView 20 android:text="*" 21 android:padding="3dip" /> 22 <TextView 23 android:text="Row12" 24 android:padding="3dip" /> 25 <TextView 26 android:text="2" 27 android:gravity="right" 28 android:padding="3dip" /> 29 </TableRow> 30 <View 31 android:layout_height="2dip" 32 android:background="#FF909090" /> 33 <TableRow> 34 <TextView 35 android:layout_column="1" 36 android:text="Row13" 37 android:padding="3dip" /> 38 </TableRow> 39 </TableLayout>
轉(zhuǎn)載于:https://www.cnblogs.com/hxb2016/p/6090286.html
總結(jié)
以上是生活随笔為你收集整理的Android_Layout (一)的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 第7章-选择器+伪类
- 下一篇: Linux使文件变成二进制,linux