Android中常用的5大布局方式有以下幾種:
線性布局(LinearLayout):按照垂直或者水平方向布局的組件虽风。
幀布局(FrameLayout):組件從屏幕左上方布局組件温鸽。
表格布局(TableLayout):按照行列方式布局組件。
相對(duì)布局(RelativeLayout):相對(duì)其它組件的布局方式秉版。
絕對(duì)布局(AbsoluteLayout):按照絕對(duì)坐標(biāo)來布局組件贤重。
1. 線性布局
線性布局是Android開發(fā)中最常見的一種布局方式,它是按照垂直或者水平方向來布局清焕,通過“android:orientation”屬性可以設(shè)置線性布局的方向并蝗。屬性值有垂直(vertical)和水平(horizontal)兩種。
常用的屬性:
android:orientation:可以設(shè)置布局的方向
android:gravity:用來控制組件的對(duì)齊方式
layout_weight:控制各個(gè)組件在布局中的相對(duì)大小
第一個(gè)實(shí)例
①效果圖:
②核心代碼如下:
main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
? ? android:orientation="vertical"
? ? android:layout_width="fill_parent"
? ? android:layout_height="fill_parent"
? ? >? ? ?
? ? <LinearLayout
? ? ? ? android:layout_width="fill_parent"
? ? ? ? android:layout_height="wrap_content"
? ? ? ? android:orientation="vertical"
? ? ? ? >
? ? ? ? <EditText
? ? ? ? ? ? android:layout_width="fill_parent"
? ? ? ? ? ? android:layout_height="wrap_content"
? ? ? ? ? ? />
? ? </LinearLayout>
? ? <LinearLayout
? ? ? ? android:layout_width="fill_parent"
? ? ? ? android:layout_height="wrap_content"
? ? ? ? android:orientation="horizontal"
? ? ? ? android:gravity="right"
? ? ? ? >
? ? <!-- android:gravity="right"表示Button組件向右對(duì)齊 -->
? ? ? ? <Button
? ? ? ? ? ? android:layout_height="wrap_content"
? ? ? ? ? ? android:layout_width="wrap_content"
? ? ? ? ? ? android:text="確定"
? ? ? ? ? ? />
? ? ? ? <Button
? ? ? ? ? ? android:layout_height="wrap_content"
? ? ? ? ? ? android:layout_width="wrap_content"
? ? ? ? ? ? android:text="取消"
? ? ? ? ? ? />? ?
? ? </LinearLayout>
</LinearLayout>
第二個(gè)實(shí)例
①效果圖:
②核心代碼:
mian.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
? ? android:orientation="vertical" android:layout_width="fill_parent"
? ? android:layout_height="fill_parent">
? ? <LinearLayout
? ? android:orientation="horizontal"
? ? android:layout_width="fill_parent"
? ? android:layout_height="fill_parent"
? ? android:layout_weight="1">
? ? <TextView
? ? ? ? android:text="red"
? ? ? ? android:gravity="center_horizontal"
? ? ? ? android:background="#aa0000"
? ? ? ? android:layout_width="wrap_content"
? ? ? ? android:layout_height="fill_parent"
? ? ? ? android:layout_weight="1"
? ? ? ? />
? ? <!--android:gravity="center_horizontal"水平居中 -->?
? ? <!--layout_weight屬性以控制各個(gè)控件在布局中的相對(duì)大小耐朴。layout_weight屬性是一個(gè)非負(fù)整數(shù)值借卧。?
? ? ? ? 線性布局會(huì)根據(jù)該控件layout_weight值與其所處布局中所有控件layout_weight值之和的比值為該控件分配占用的區(qū)域盹憎。?
? ? ? ? 例如筛峭,在水平布局的LinearLayout中有兩個(gè)Button,這兩個(gè)Button的layout_weight屬性值都為1陪每,?
? ? ? ? 那么這兩個(gè)按鈕都會(huì)被拉伸到整個(gè)屏幕寬度的一半影晓。如果layout_weight指為0,控件會(huì)按原大小顯示檩禾,不會(huì)被拉伸挂签;?
? ? ? ? 對(duì)于其余l(xiāng)ayout_weight屬性值大于0的控件,系統(tǒng)將會(huì)減去layout_weight屬性值為0的控件的寬度或者高度盼产,?
? ? ? ? 再用剩余的寬度或高度按相應(yīng)的比例來分配每一個(gè)控件顯示的寬度或高度-->
? ? <TextView
? ? ? ? android:text="Teal"
? ? ? ? android:gravity="center_horizontal"
? ? ? ? android:background="#008080"
? ? ? ? android:layout_width="wrap_content"
? ? ? ? android:layout_height="fill_parent"
? ? ? ? android:layout_weight="1"/>
? ? <TextView
? ? ? ? android:text="blue"
? ? ? ? android:gravity="center_horizontal"
? ? ? ? android:background="#0000aa"
? ? ? ? android:layout_width="wrap_content"
? ? ? ? android:layout_height="fill_parent"
? ? ? ? android:layout_weight="1"
? ? ? ? />
? ? <TextView
? ? ? ? android:text="orange"
? ? ? ? android:gravity="center_horizontal"
? ? ? ? android:background="#FFA500"
? ? ? ? android:layout_width="wrap_content"
? ? ? ? android:layout_height="fill_parent"
? ? ? ? android:layout_weight="1"
? ? ? ? />
? ? </LinearLayout>?
? ? <LinearLayout
? ? android:orientation="vertical"
? ? android:layout_width="fill_parent"
? ? android:layout_height="fill_parent"
? ? android:layout_weight="1">
? ? <TextView
? ? ? ? android:text="row one"
? ? ? ? android:textSize="15pt"
? ? ? ? android:background="#aa0000"
? ? ? ? android:layout_width="fill_parent"
? ? ? ? android:layout_height="wrap_content"
? ? ? ? android:layout_weight="1"
? ? ? ? />
? ? <!--? -->?
? ? <TextView
? ? ? ? android:text="row two"
? ? ? ? android:textSize="15pt"
? ? ? ? android:background="#DDA0DD"
? ? ? ? android:layout_width="fill_parent"
? ? ? ? android:layout_height="wrap_content"
? ? ? ? android:layout_weight="1"
? ? ? ? />
? ? <TextView
? ? ? ? android:text="row three"
? ? ? ? android:textSize="15pt"
? ? ? ? android:background="#008080"
? ? ? ? android:layout_width="fill_parent"
? ? ? ? android:layout_height="wrap_content"
? ? ? ? android:layout_weight="1"
? ? ? ? />? ?
? ? <TextView
? ? ? ? android:text="row four"
? ? ? ? android:textSize="15pt"
? ? ? ? android:background="#FFA500"
? ? ? ? android:layout_width="fill_parent"
? ? ? ? android:layout_height="wrap_content"
? ? ? ? android:layout_weight="1"
? ? ? ? />? ? ?
? ? </LinearLayout>?
</LinearLayout>
2. 幀布局
幀布局是從屏幕的左上角(0,0)坐標(biāo)開始布局饵婆,多個(gè)組件層疊排列,第一個(gè)添加的組件放到最底層戏售,最后添加到框架中的視圖顯示在最上面侨核。上一層的會(huì)覆蓋下一層的控件草穆。
簡(jiǎn)單的例子
①效果圖:
② 核心代碼:
main.xml
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
? ? android:layout_width="fill_parent"
? ? android:layout_height="fill_parent"
? ? >
? ? <TextView? ?
? ? ? ? android:layout_width="300dp"?
? ? ? ? android:layout_height="300dp"?
? ? ? ? android:background="#00BFFF"? ? ? ? ?
? ? ? ? />
? ? <TextView? ?
? ? ? ? android:layout_width="260dp"?
? ? ? ? android:layout_height="260dp"?
? ? ? ? android:background="#FFC0CB"? ? ? ? ?
? ? ? ? />
? ? <TextView? ?
? ? ? ? android:layout_width="220dp"?
? ? ? ? android:layout_height="220dp"?
? ? ? ? android:background="#0000FF"? ? ? ? ?
? ? ? ? />
</FrameLayout>
3.表格布局
表格布局是一個(gè)ViewGroup以表格顯示它的子視圖(view)元素,即行和列標(biāo)識(shí)一個(gè)視圖的位置搓译。
表格布局常用的屬性如下:
android:collapseColumns:隱藏指定的列
android:shrinkColumns:收縮指定的列以適合屏幕悲柱,不會(huì)擠出屏幕
android:stretchColumns:盡量把指定的列填充空白部分
android:layout_column:控件放在指定的列
android:layout_span:該控件所跨越的列數(shù)
簡(jiǎn)單的列子:
①效果圖:
② 核心代碼:
main.xml
<?xml version="1.0" encoding="utf-8"?>
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
? ? android:layout_width="fill_parent"
? ? android:layout_height="fill_parent"
? ? >
? ? <TableRow>
? ? ? ? <Button
? ? ? ? ? ? android:text="Button1"
? ? ? ? ? ? />
? ? ? ? <Button
? ? ? ? ? ? android:text="Button2"
? ? ? ? ? ? />
? ? ? ? <Button
? ? ? ? ? ? android:text="Button3"
? ? ? ? ? ? />
? ? </TableRow>
? ? <TableRow>
? ? ? ? <Button
? ? ? ? ? ? android:text="Button4"
? ? ? ? ? ? />
? ? ? ? <Button
? ? ? ? ? ? android:layout_span="2"
? ? ? ? ? ? android:text="Button5"
? ? ? ? ? ? />
? ? </TableRow>
</TableLayout>
4.相對(duì)布局
相對(duì)布局是按照組件之間的相對(duì)位置來布局,比如在某個(gè)組件的左邊些己,右邊豌鸡,上面和下面等。
相對(duì)布局常用屬性請(qǐng)參考我博客的:http://liangruijun.blog.51cto.com/3061169/631816
簡(jiǎn)單的例子
①效果圖:
② 核心代碼:
main.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
? ? android:layout_width="fill_parent"
? ? android:layout_height="wrap_content"
? ? android:padding="10px"
? ? >
? ? <TextView? ?
? ? ? ? android:id="@+id/tev1"
? ? ? ? android:layout_width="wrap_content"?
? ? ? ? android:layout_height="wrap_content"?
? ? ? ? android:layout_marginBottom="30dp"
? ? ? ? android:text="Please Type Here:"
? ? ? ? />
? ? <EditText
? ? ? ? android:id="@+id/tx1"
? ? ? ? android:layout_width="match_parent"
? ? ? ? android:layout_height="wrap_content"
? ? ? ? android:layout_below="@id/tev1"
? ? ? ? />
? ? <Button
? ? ? ? android:id="@+id/btn1"
? ? ? ? android:layout_height="wrap_content"
? ? ? ? android:layout_width="wrap_content"
? ? ? ? android:layout_below="@id/tx1"
? ? ? ? android:layout_alignParentRight="true"
? ? ? ? android:text="確定"
? ? ? ? />
? ? <Button
? ? ? ? android:id="@+id/btn2"
? ? ? ? android:layout_height="wrap_content"
? ? ? ? android:layout_width="wrap_content"
? ? ? ? android:layout_below="@id/tx1"
? ? ? ? android:layout_toLeftOf="@id/btn1"
? ? ? ? android:layout_marginRight="30dp"
? ? ? ? android:text="取消"
? ? ? ? />
</RelativeLayout>
東莞網(wǎng)站建設(shè)www.zg886.cn