Android - 布局详解之LinearLayout与RelativeLayout
生活随笔
收集整理的這篇文章主要介紹了
Android - 布局详解之LinearLayout与RelativeLayout
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
本篇博文只針對LinearLayout與RelativeLayout
我們在新建一個布局文件時,一般遵循這樣的思路:先確定該文件對應的界面中各個布局和控件的位置和大小,然后再來設置各個布局和控件的其他屬性,如背景、文字等。
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ??上篇 ? ?確定控件的位置和尺寸
在確定各個布局和控件的位置和大小時,首先需要考慮的是最外層的Layout的位置,有如下兩種方法可以采用:
1、直接設置最外層的Layout填充父窗體,即: android:layout_width="match_parent" android:layout_height="match_parent" ?, ?這將不涉及Layout與父窗體邊緣的距離設置。
2、設置最外層Layout的長寬中的一個或兩個的值剛好適應子布局和控件的尺寸,即: 設置android:layout_width=" ?"、android:layout_height=" ?"中的一個或兩個屬性的值為wrap_content, 這種情況下,Layout默認是左邊緣和上邊緣與父窗體對齊,如果想改變它的顯示位置,可使用如下幾種方式:
A、直接用一個具體的值來指定Layout與父窗體邊緣的距離,需要使用這幾個屬性:
| android:layout_marginLeft | 設置該Layout距離父窗體左邊緣的距離 |
| android:layout_marginTop | 設置該Layout距離父窗體上邊緣的距離 |
上述解決了Layout在父窗體中的位置和大小設置的問題,LinearLayout與RelativeLayout皆適用。 那么,一個?LinearLayout或RelativeLayout中的控件(布局)的位置?又是怎么來確定呢?
在界面比較復雜的情況下,我們可以先將這個Layout中的所有子Layout和控件都視為子控件,待處理完這個Layout中的子控件的位置后,再來處理子Layout中的控件的位置和大小,有點類似遞歸的思想。
同樣,在設置Layout中的控件的位置時,LinearLayout與RelativeLayout也有很多可以共用的屬性, 比如使用如下幾種方式來設置: A、直接用一個具體的值來指定Layout中的內容與Layout邊緣的距離,需要使用這幾個屬性:
| android:paddingLeft | 設置該Layout中的內容距離該Layout左邊緣的距離 |
| android:paddingTop | 設置該Layout中的內容距離該Layout上邊緣的距離 |
| android:paddingRight | 設置該Layout中的內容距離該Layout右邊緣的距離 |
| android:paddingBottom | 設置該Layout中的內容距離該Layout下邊緣的距離 |
B、用非具體數值的方式來確定該Layout中的內容的位置,需要使用以下屬性: android:gravity ? ? ? ??? ? ? ? ? ? ? ? ? ? ? ? ? ? ? 可以選擇的常用值有:center_vertical、center_horizontal、right(豎直居中、水平居中、靠右)等 但需要注意的是,使用A、B兩種方式是會有沖突的,這會增加維護的難度,最好不要同時使用。
除此之外,在設置Layout中的控件的位置時,LinearLayout與RelativeLayout也有很多不同的屬性。 接下來分析,LinearLayout與RelativeLayout的屬性有何不同?在使用時怎么選擇?
一、LinearLayout
LinearLayout的注釋: /*** A Layout that arranges its children in a single column or a single row. The direction of * the row can be set by calling {@link #setOrientation(int) setOrientation()}. * You can also specify gravity, which specifies the alignment of all the child elements by* calling {@link #setGravity(int) setGravity()} or specify that specific children * grow to fill up any remaining space in the layout by setting the <em>weight</em> member of* {@link android.widget.LinearLayout.LayoutParams LinearLayout.LayoutParams}.* The default orientation is horizontal.*/ //LinearLayout 將它的子布局(控件)排成一行或一列,可以調用setOrientation()方法來設置排列 //的方向,或者調用setGravity()方法 //來指定子布局(控件)的對齊方式,還可以設置weight的值 //來改變子布局(控件)的填充范圍,默認排列方向是horizontal public class LinearLayout extends ViewGroup {} 在設置Layout里邊的控件的位置時,LinearLayout中的控件可以使用的屬性有:
A、設置該控件距左、上、右、下邊(無論是父控件還是兄弟控件)的長度(在RelativeLayout中也適用):
| android:layout_marginLeft | 設置該控件距左邊(無論是父控件還是兄弟控件)的長度 |
| android:layout_marginTop | 設置該控件距上邊(無論是父控件還是兄弟控件)的長度 |
| android:layout_marginRight | 設置該控件距右邊(無論是父控件還是兄弟控件)的長度 |
| android:layout_marginBottom | 設置該控件距下邊(無論是父控件還是兄弟控件)的長度 |
二、RelativeLayout
RelativeLayout的注釋: /*** A Layout where the positions of the children can be described in relation to each other or to the* parent.*/ //在RelativeLayout中,可以依據一個子布局(控件)與其他子布局(控件) //或者父窗體的相對位置關系來描述它的位置 public class RelativeLayout extends ViewGroup {} 在設置Layout里邊的控件的位置時,RelativeLayout中的控件可以使用的屬性有:
A、設置該控件和父控件的相對位置(屬性值為true或false):
| android:layout_centerHrizontal | 水平居中 |
| android:layout_centerVertical | 垂直居中 |
| android:layout_centerInparent | 相對于父元素完全居中 |
| android:layout_alignParentBottom | 貼緊父元素的下邊緣 |
| android:layout_alignParentLeft | 貼緊父元素的左邊緣 |
| android:layout_alignParentRight | 貼緊父元素的右邊緣 |
| android:layout_alignParentTop | 貼緊父元素的上邊緣 |
B、設置該控件和某個兄弟控件的相對位置(屬性值為控件的id):
| android:layout_below | 在某元素的下方 |
| android:layout_above | 在某元素的的上方 |
| android:layout_toLeftOf | 在某元素的左邊 |
| android:layout_toRightOf | 在某元素的右邊 |
| android:layout_alignBaseline | 本元素的baseline和給定元素的baseline對齊 |
| android:layout_alignTop | 本元素的上邊緣和某元素的的上邊緣對齊 |
| android:layout_alignLeft | 本元素的左邊緣和某元素的的左邊緣對齊 |
| android:layout_alignBottom | 本元素的下邊緣和某元素的的下邊緣對齊 |
| android:layout_alignRight | 本元素的右邊緣和某元素的的右邊緣對齊 |
C、設置該控件距左、上、右、下邊(無論是父控件還是兄弟控件)的長度(屬性值為具體值如30dip,在LinearLayout中也適用):
| android:layout_marginLeft | 設置該控件距左邊(無論是父控件還是兄弟控件)的長度 |
| android:layout_marginTop | 設置該控件距上邊(無論是父控件還是兄弟控件)的長度 |
| android:layout_marginRight | 設置該控件距右邊(無論是父控件還是兄弟控件)的長度 |
| android:layout_marginBottom | 設置該控件距下邊(無論是父控件還是兄弟控件)的長度 |
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?下篇 ? ?確定控件的其他屬性
以下為各個控件的常用屬性總結:
(待補充)
轉載于:https://www.cnblogs.com/hwgt/p/5414401.html
總結
以上是生活随笔為你收集整理的Android - 布局详解之LinearLayout与RelativeLayout的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: http的get与post方式下的get
- 下一篇: 去苹果浏览器默认样式