在使用布局前我們需要瞭解兩個屬性:
豎直方向布局:android:layout_height="wrap_content"
水平方向布局:android:layout_width="match_parent"
wrap_content表示包裹內容而不填充,match_parent表示鋪滿父容器,fill_parent和match _parent的意思一樣 ,在2.2以上兩個詞都可以用,2.2以下的話,用fill_parent。
使用FrameLayout布局
FrameLayout對象就好比在一塊螢幕上提前一定好的空白地區,然後可以填充元素在裡面。所有元素被放置在地區的最左上,而且無法為這些元素制定確定的位置
<FrameLayout android:id="@+id/frameLayout1"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<Button android:layout_height="wrap_content" android:text="@string/ok" android:id="@+id/button1" android:layout_width="match_parent"></Button>
<Button android:text="Button" android:id="@+id/button2" android:layout_width="wrap_content" android:layout_height="wrap_content"></Button>
</FrameLayout>
當放置了兩個按鈕,兩個按鈕會疊加在一起
使用LinearLayout布局
在android中常用的布局方式,它將自己包含的子項目按照一個方向(水平或垂直)進行排列
<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:orientation="vertical"或 android:orientation="horizontal"來設定水平或是垂直排列
使用RelativeLayout布局
這個表示相對布局,它裡面的元素按照相對位置來計算的。可以指定一個內部的元素A相對於元素B的位置
anroid中也有padding 、margin的概念。padding表示填充,margin表示邊距。
布局設計到和父元素的對齊,和相對元素的對齊 ,間距等。
1.和父元素對齊屬性:
android:layout_centerHrizontal 水平置中
android:layout_centerVertical 垂直置中
android:layout_centerInparent 相對於父元素完全置中
android:layout_alignParentBottom 貼緊父元素的下邊緣
android:layout_alignParentLeft 貼緊父元素的左邊緣
android:layout_alignParentRight 貼緊父元素的右邊緣
android:layout_alignParentTop 貼緊父元素的上邊緣
android:layout_alignWithParentIfMissing 如果對應的兄弟元素找不到的話就以父元素做參照物
2.和相對元素對齊屬性:
android:layout_below 在某元素的下方
android:layout_above 在某元素的的上方
android:layout_toLeftOf 在某元素的右邊
android:layout_alignTop 本元素的上邊緣和某元素的的上邊緣對齊
android:layout_alignLeft 本元素的左邊緣和某元素的的左邊緣對齊
android:layout_alignBottom 本元素的下邊緣和某元素的的下邊緣對齊
android:layout_alignRight 本元素的右邊緣和某元素的的右邊緣對齊
3.間距屬性:
android:layout_marginBottom 離某元素底邊緣的距離
android:layout_marginLeft 離某元素左邊緣的距離
android:layout_marginRight 離某元素右邊緣的距離
android:layout_marginTop 離某元素上邊緣的距離
android中度量單位有:
px 像素,dip依賴於裝置的像素,sp 帶比例的像素 ,pt 點 ,in 英寸,mm 毫米
使用TableLayout布局
是一種表格版面配置,以行和列的形式排列。它裡麵包含了TableRow定義了每一行,每一行裡可以添加需要的元素
<TableLayout android:id="@+id/tableLayout1" android:layout_width="match_parent" android:layout_height="wrap_content">
<TableRow android:id="@+id/tableRow1" android:layout_width="wrap_content" android:layout_height="wrap_content">
<TextView android:id="@+id/textView1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="TextView"></TextView>
<EditText android:text="EditText" android:id="@+id/editText1" android:layout_width="wrap_content" android:layout_height="30px"></EditText>
</TableRow>
<TableRow android:id="@+id/tableRow2" android:layout_width="wrap_content" android:layout_height="wrap_content">
<TextView android:id="@+id/textView1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="TextView"></TextView>
<EditText android:text="EditText" android:id="@+id/editText1" android:layout_width="wrap_content" android:layout_height="30px"></EditText>
</TableRow>
</TableLayout>