安卓基礎知識回顧------Android中的五大布局
阿新 • • 發佈:2019-02-10
Android中的五大布局
在android中的佈局有五大類,這五種佈局為別為:LinearLayout(線性佈局),FrameLayout(框架佈局),RelativeLayout(相對佈局),TableLayout(表格佈局),AbsoluteLayout(座標佈局) 我們目前的學習中主要運用到的是LinearLayout(線性佈局)和RelativeLayout(相對佈局)
LinearLayout:
被稱為線性佈局,分為水平和垂直,設定的垂直或水平的屬性值,來排列所有的子元素。所有的子元素都被堆放在其它元素之後,因此一個垂直列表的每一行只會有一個元素,而不管他們有多寬,而一個水平列表將會只有一個行高(高度為最高子元素的高度加上邊框高度)。LinearLayout保持子元素之間的間隔以及互相對齊(相對一個元素的右對齊、中間對齊或者左對齊)。
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height ="250dp"
android:orientation="horizontal">
<TextView
android:layout_width="96dp"
android:layout_height="match_parent"
android:background="#b2dfdb" />
</LinearLayout>
RelativeLayout:
相對佈局,可指定某元素相對於其他的元素的位置,可以通過layout_below=”相對控制元件”。可以以右對齊,或上下,或置於螢幕中央的形式來排列兩個元素。元素按順序排列,因此如果第一個元素在螢幕的中央,那麼相對於這個元素的其它元素將以螢幕中央的相對位置來排列。如果使用XML 來指定這個 layout ,在你定義它之前,被關聯的元素必須定義。
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<TextView
style="@style/btn_relative"
android:text="1" />
<TextView
style="@style/btn_relative"
android:text="2" />
</RelativeLayout>