android開發之二layout

我們有兩種方式聲明佈局:

1,在XML中聲明。

2,在程序中動態聲明。

android我目前所學習到的佈局方式有以下幾種:

1. LineraLayout局部線性佈局

感覺這個佈局方式是最常見的一種。它有兩種佈局方式:行列。是由參數orientation(方位)決定的。vertical表示豎直排列,horizontal表示橫向排列.

具體代碼如

  <LinearLayoutxmlns:android="http://schemas.android.com/apk/res/android"
 android:orientation="vertical" 
android:layout_width="fill_parent"
 android:layout_height="fill_parent">

 每個參數都要以android:開頭。一般都有android:layout_widthandroid:layout_height 具體的值有兩個一個是fill_parent 一個是wrap_content  每一個佈局都是一個容器,layout_widthheight表示這個容器的寬 高是填充滿父容器,還是依賴於他本身的內容

2. FrameLayout佈局框架佈局

這個佈局一般是放圖片的,放入其中的所有元素都被放置在FrameLayout區域最左上的區域,而且無法爲這些元素指定一個確切的位置。如果一個FrameLayout裏有多個子元素,那麼後邊的子元素的顯示會重疊在前一個元素上。這個佈局方式的寬與高的參數一般是用wrap_content

3. RelativeLayout佈局相對佈局

這個佈局很常見。具體代碼比如:

<RelativeLayout
 xmlns:android="
http://schemas.android.com/apk/res/android"
 android:layout_width="fill_parent"android:layout_height="wrap_content"
 android:background="@drawable/blue"android:padding="10dip">

 <TextViewandroid:id="@+id/label" android:layout_width="fill_parent"
  android:layout_height="wrap_content" android:text="
請輸入用戶名:" />

 <!--
  
這個EditText放置在上邊idlabelTextView的下邊
 -->
 <EditText android:id="@+id/entry"android:layout_width="fill_parent"
  android:layout_height="wrap_content"
  android:background="@android:drawable/editbox_background"
  android:layout_below="@id/label" />

 <!--
  
取消按鈕和容器的右邊齊平,並且設置左邊的邊距爲10dip
 -->
 <Button android:id="@+id/cancel"android:layout_width="wrap_content"
  android:layout_height="wrap_content"android:layout_below="@id/entry"
  android:layout_alignParentRight="true"
  android:layout_marginLeft="10dip" android:text="
取消" />

 <!--
  
確定按鈕在取消按鈕的左側,並且和取消按鈕的高度齊平
 -->
 <Button android:id="@+id/ok"android:layout_width="wrap_content"
  android:layout_height="wrap_content"
  android:layout_toLeftOf="@id/cancel"
  android:layout_alignTop="@id/cancel" android:text="
確定" />

</RelativeLayout>
在這個容器裏放置了一些組件,每個組件裏多了一些對他們位置關係的描述,比如android:layout_below="@id/label"表示放在idlabel的控件下面== 要注意的是,如果組件B依賴於A,那麼必須要讓A出現在B的前面。

padding表示填充,margin表示邊距。android當中最常見的支持描述大小區域的類型如下 px像素 dip依賴於設備的像素.

4. TableLayout 表格佈局

<TableLayoutxmlns:android="http://schemas.android.com/apk/res/android"
 android:layout_width="fill_parent"android:layout_height="fill_parent"
 android:stretchColumns="1">

 <TableRow>
  <TextView android:text="
用戶名:"android:textStyle="bold"
   android:gravity="right"android:padding="3dip" />

  <EditTextandroid:id="@+id/username" android:padding="3dip"
   android:scrollHorizontally="true" />
 </TableRow>

 <TableRow>
  <TextView android:text="
登錄密碼:"android:textStyle="bold"
   android:gravity="right"android:padding="3dip" />

  <EditTextandroid:id="@+id/password" android:password="true"
   android:padding="3dip"android:scrollHorizontally="true" />
 </TableRow>

 <TableRowandroid:gravity="right">

  <Buttonandroid:id="@+id/cancel"
   android:text="
取消"/>

  <Buttonandroid:id="@+id/login"
   android:text="
登錄"  android:layout_width="wrap_content"
   android:layout_height="wrap_content"/>
 </TableRow>
</TableLayout>

裏面是由多個TableRow組成的。一行一行的很好理解。

佈局方式是可以組合在一起的。這個也不難理解。

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章