布局管理器存在的意義
不同的安卓手機,其分辨率、尺寸存在差異,為了能使Android應(yīng)用的用戶界面具有平臺無關(guān)性,Android提供了布局管理器用于管理TextView、Button等組件(布局管理器可根據(jù)運行平臺來調(diào)整組件的大?。?br> ViewGroup繼承于View,它管理著Button等View組件的位置及大小,而其本身也是一個View組件(因此某個布局管理器可以嵌套到另一個布局管理器中)。
如何用布局管理器管理組件
方法一:直接使用xml布局文件,如下面的代碼就在id為root的線性布局管理器(LinearLayout)中定義了一個Button
main.xml
<LinearLayout
android:id="@+id/root"
android:orientation="vertical">
<Button
android:id="@+id/button
android:layout_width="wrap_content"
android:layout_height="fill_parent"/>
</LinearLayout>
方法二:xml和java代碼混合使用,即先在xml中定義一個布局管理器,接著在java代碼中使用addView()方法添加組件,實例代碼如下:
main.xml
<RelativeLayout
android:id="@+id"=root
android:layout_width="match_parent"
android:layout_height="match_parent">
</RelativeLayoout>
main_activity.java(代碼如有遮擋請拖動代碼塊查看)
setContentView(R.layout.main);//使main.xml中的布局能顯示出來
LinearLayout bowl=(ViewGroup)findViewById(R.id.root);
Button bn=new Button(this);
bn.setText(R.string.HelloWord);
root.addView(bn);//添加組件
布局管理器種類
布局管理器有以下種類:LinearLayout(線性布局)、RelativeLayout(相對布局)、TableLayout(表格布局)、幀布局(FrameLayout)、網(wǎng)格布局(GridLayout)
LinearLayout 線性布局
線性布局將組件一個接一個排列起來,橫向或是縱向排列由屬性值android:orientation控制,實例如下:
android:orientation="vertical"http://組件縱向排列
android:orientation="horizontal"http://組件橫向排列
注意:線性布局不會換行,當組件一個接一個排列到頭后,剩下的組件將不會被顯示出來
還有一個比較重要的屬性是android:gravity,它用于設(shè)置內(nèi)部各組件的對齊方式,實例如下:
android:gravity="center_horizontal //組件水平居中
android:gravity="right|center_vertical //組件出現(xiàn)在屏幕右方且垂直居中
TableLayout表格布局
表格布局采用行和列的方式管理組件,行數(shù)和列數(shù)通過添加TableRow和各組件來確定,示例如下:
<?xml version="1.0" encoding="utf-8"?>
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<Button android:id="@+id/bn1"http://按鈕bn1占第一行
......
android:text="bn1"/>
<TableRow>//TableRow占第二行
<Button android:id="@+id/bn2" //bn2占據(jù)第二行的第一列
......
android:text="bn2"/>
<Button android:id="@+id/bn3" //bn3占據(jù)第二行的第二列
......
android:text="bn3"/>
</TableRow>
</TableLayout>
RelativeLayout相對布局
顧名思義,相對布局中的組件的位置相對其他組件來決定,示例如下:
<TextView android:layout_toRightOf="@id/bn1"http://該文本框位于id為bn1的按鈕的右邊
android:layout_alignTop="@id/bn1 //該文本框與bn1頂部對齊
android:layout_below="@id/bn2"/>"http://該文本框位于id為bn2的文本框的底部
實戰(zhàn)
設(shè)計簡單計算器的用戶界面:
<?xml version="1.0" encoding="utf-8"?>
<GridLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:rowCount="9"
android:columnCount="4"
android:id="@+id/root"
tools:context="com.golfer.www.caculator.MainActivity">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="30pt"
android:text="0"
android:layout_rowSpan="4"
android:layout_columnSpan="4"
android:background="@drawable/textview"
android:layout_marginBottom="16pt"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="10pt"
android:layout_marginEnd="2pt"
android:text="C"
android:layout_marginLeft="2pt"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="10pt"
android:layout_marginEnd="2pt"
android:text="minus"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="10pt"
android:layout_marginEnd="2pt"
android:text="Del"/>
<Button
android:text="/"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="10pt"
android:layout_marginEnd="2pt"/>
<Button
android:text="7"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="10pt"
android:layout_marginEnd="2pt"
android:layout_marginLeft="2pt"/>
<Button
android:text="8"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="10pt"
android:layout_marginEnd="2pt"/>
<Button
android:text="9"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="10pt"
android:layout_marginEnd="2pt"/>
<Button
android:text="X"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="10pt"
android:layout_marginEnd="2pt"/>
<Button
android:text="4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="10pt"
android:layout_marginEnd="2pt"
android:layout_marginLeft="2pt"/>
<Button
android:text="5"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="10pt"
android:layout_marginEnd="2pt"/>
<Button
android:text="6"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="10pt"
android:layout_marginEnd="2pt"/>
<Button
android:text="—"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="10pt"
android:layout_marginEnd="2pt"/>
<Button
android:text="1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="10pt"
android:layout_marginEnd="2pt"
android:layout_marginLeft="2pt"/>
<Button
android:text="2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="10pt"
android:layout_marginEnd="2pt"/>
<Button
android:text="3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="10pt"
android:layout_marginEnd="2pt"/>
<Button
android:text="+"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="10pt"
android:layout_marginEnd="2pt"/>
<Button
android:text="0"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="10pt"
android:layout_marginEnd="2pt"/>
<Button
android:text="."
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="10pt"
android:layout_marginEnd="2pt"/>
<Button
android:layout_height="wrap_content"
android:layout_columnSpan="2"
android:text="="
android:layout_width="80pt" />
</GridLayout>
界面如下圖:

需要指出的是,這樣直接在xml布局文件中定義各數(shù)字按鈕顯得代碼冗余,我們應(yīng)當采取xml布局文件和java代碼混合的方式