佈局就是指android中如何擺放組件,android主要使用的佈局有線性佈局,絕對佈局,相對佈局,表格佈局,框架佈局這幾種形式,下面首先來介紹一下,線性佈局
一、線性佈局
對於線性佈局來說,就是指這個組件會在實際擺放時單獨佔用一行
具體擺放形式如下,關鍵字是linearlayout,以這個關鍵字開頭就是線性佈局,此佈局下各組件會單獨佔用一行的空間,共有兩種形式,水平和垂直,定義水平還是垂直需要用到android:orientation=”horizontal”關鍵字,horizontal是垂直,vertical即爲水平
水平代碼如下
<LinearLayout xmlns:tools="http://schemas.android.com/tools"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"<!-- 水平-->
tools:context="com.example.helloandroid.SecondActivity" xmlns:android="http://schemas.android.com/apk/res/android">
<TextView
android:id="@id/textViewSet1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center_vertical"
android:text="I am Second" />
<CheckBox
android:id="@+id/checkBox1"
android:gravity="center_vertical"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@id/textViewSet1"
android:layout_alignParentTop="true"
android:text="CheckBox" />
<Button
android:id="@+id/buttonclose1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center_vertical"
android:text="Button" />
</LinearLayout>
垂直代碼如下
<LinearLayout xmlns:tools="http://schemas.android.com/tools"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="horizontal"
tools:context="com.example.helloandroid.SecondActivity" xmlns:android="http://schemas.android.com/apk/res/android">
<TextView
android:id="@id/textViewSet1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center_horizontal"
android:text="I am Second" />
<CheckBox
android:id="@+id/checkBox1"
android:gravity="center_horizontal"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@id/textViewSet1"
android:layout_alignParentTop="true"
android:text="CheckBox" />
<Button
android:id="@+id/buttonclose1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center_horizontal"
android:text="Button" />
</LinearLayout>
二、絕對佈局
和名稱一樣,絕對佈局就是通過設置每個組件的x,y來進行擺放的佈局,關鍵字爲AbsouluteLayout,需要設置的兩個屬性爲android:layout_x和android:layout_y
示例代碼:
<AbsoluteLayout xmlns:tools="http://schemas.android.com/tools"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
tools:context="com.example.helloandroid.SecondActivity" xmlns:android="http://schemas.android.com/apk/res/android">
<TextView
android:id="@id/textViewSet1"
android:layout_x="32pt"
android:layout_y="32pt"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center_horizontal"
android:text="I am Second" />
<Button
android:id="@+id/buttonclose1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_x="76dp"
android:layout_y="4dp"
android:gravity="center_horizontal"
android:text="Button" />
<CheckBox
android:id="@+id/checkBox1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_x="63dp"
android:layout_y="114dp"
android:text="CheckBox" />
</AbsoluteLayout>
三.相對佈局
相對佈局就是指其佈局依賴於各組件,位置排放與參照的組件位置有關,相對佈局的關鍵字爲RelativeLayout,使用這個關鍵字後就代表此Activity的佈局是相對佈局,相對佈局就需要設置如下屬性
android:layout_below=”組件id”在該組件下方
android:layout_above=”組件id”在該組件上方
android:layout_toRightOf=”組件id”在該組件右方
android:layout_toLeftOf=”組件id”在該組件左方
參照代碼:
<RelativeLayout xmlns:tools="http://schemas.android.com/tools"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
tools:context="com.example.helloandroid.SecondActivity" xmlns:android="http://schemas.android.com/apk/res/android">
<TextView
android:id="@id/textViewSet1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center_horizontal"
android:text="I am Second" />
<CheckBox
android:id="@+id/checkBox1"
android:layout_below="@+id/buttonclose1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="CheckBox" />
<Button
android:id="@+id/buttonclose1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="@id/textViewSet1"
android:text="Button" />
</RelativeLayout>
四,表格佈局
表格佈局就相當於html中的table標籤,在這裏使用的關鍵字是TableLayout,此時組件相當於在一個表格中進行擺放,設置TableLayout後就需要設置以下屬性
TableRow:行
表格佈局的代碼:
<TableLayout xmlns:tools="http://schemas.android.com/tools"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
tools:context="com.example.helloandroid.SecondActivity" xmlns:android="http://schemas.android.com/apk/res/android">
<TableRow>
<TextView
android:id="@id/textViewSet1"
android:layout_weight="1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="I am Second" />
</TableRow>
<TableRow>
<CheckBox
android:id="@+id/checkBox1"
android:layout_weight="2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="CheckBox" />
</TableRow>
<TableRow>
<Button
android:id="@+id/buttonclose1"
android:layout_weight="3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:text="Button" />
</TableRow>
</TableLayout>
五.框架佈局
此佈局主要用來存放一個組件,如果有多個組件會按照順序顯示最後一個,其他會被覆蓋,但實際依然存在,示例代碼如下,關鍵字爲FrameLayout
<FrameLayout xmlns:tools="http://schemas.android.com/tools"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
tools:context="com.example.helloandroid.SecondActivity" xmlns:android="http://schemas.android.com/apk/res/android">
<TextView
android:id="@id/textViewSet1"
android:layout_weight="1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="I am Second" />
<CheckBox
android:id="@+id/checkBox1"
android:layout_weight="2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="CheckBox" />
<Button
android:id="@+id/buttonclose1"
android:layout_weight="3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:text="Button" />
</FrameLayout>
這就是android主要使用的5種佈局,另外這5種佈局可以相互嵌套使用,和html的標籤是類似的