Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲
您现在的位置: Android教程網 >> Android系統教程 >  >> Android 技術 >> Android 開發實例

Android 開發系列(五):App 佈局初探——六種常見簡易模型

編輯:Android 開發實例

科技進步源自探索,而探索來自對本源的追問。App 佈局雖沒那麼玄妙,本文也只是把常見結構做一次歸類與總結,供 Android 開發者參考。原則不變:簡單、靈活。

縱觀市面上的 Android 應用,介面結構來來去去總逃脫不了那幾種熟悉的樣式。以下依實務經驗歸納出六種常見佈局模型,並附上對應的 layout 程式碼。

模型一:水平三列坐擁式

左右兩側分別是「返回」與「提交」按鈕,中間是必須置中的標題文字,猶如標題被左右兩顆按鈕「坐擁入懷」。主要使用 FrameLayout。

<!--缺點:標題字一多就會撐開並與兩側按鈕重疊;優點:程式碼簡潔-->
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="https://schemas.android.com/apk/res/android"
 android:layout_width="fill_parent"
 android:layout_height="wrap_content"
 android:background="@drawable/layout01_bg"
 android:paddingLeft="10dip"
 android:paddingRight="10dip">
 <Button android:layout_gravity="left|center_vertical"
  android:layout_width="wrap_content" android:layout_height="wrap_content"
  android:background="@drawable/layout01_tool" android:text="返回" android:padding="8dip" />
 <TextView android:layout_gravity="center"
  android:layout_width="wrap_content" android:layout_height="wrap_content"
  android:text="標題內容" android:textSize="18dip" android:textColor="#000000" />
 <Button android:layout_gravity="right|center_vertical"
  android:layout_width="wrap_content" android:layout_height="wrap_content"
  android:background="@drawable/layout01_tool" android:text="前進" android:padding="8dip" />
</FrameLayout>

模型二:水平三列雙耳式

與坐擁式相似,但中間文字可獨立佔位、置左置中置右皆可,不會與兩側按鈕重疊,靈活度更高。此模型以 RelativeLayout 實作較佳,需定義 ID,稍嫌繁瑣。

<!--缺點:比坐擁式稍複雜;優點:更強大、更靈活-->
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="https://schemas.android.com/apk/res/android"
 android:layout_width="fill_parent" android:layout_height="wrap_content"
 android:background="@drawable/layout01_bg"
 android:paddingLeft="10dip" android:paddingRight="10dip">
 <Button android:id="@+id/left_button"
  android:layout_alignParentLeft="true" android:layout_centerVertical="true"
  android:layout_width="wrap_content" android:layout_height="wrap_content"
  android:background="@drawable/layout01_tool" android:text="返回列表" android:padding="8dip" />
 <Button android:id="@+id/right_button"
  android:layout_alignParentRight="true" android:layout_centerVertical="true"
  android:layout_width="wrap_content" android:layout_height="wrap_content"
  android:background="@drawable/layout01_tool" android:text="評論" android:padding="8dip" />
 <!--以 LeftOf 與 RightOf 填滿中間空餘部分-->
 <TextView android:layout_toRightOf="@id/left_button"
  android:layout_toLeftOf="@id/right_button" android:layout_centerVertical="true"
  android:gravity="left" android:paddingLeft="5dip"
  android:layout_width="fill_parent" android:layout_height="wrap_content"
  android:text="資訊>>正文" android:textSize="18dip" android:textColor="#000000" />
</RelativeLayout>

補充一點:許多人以為用 LinearLayout、兩側控件靠左右、中間用 layout_gravity 調整即可,但當 LinearLayout 方向設為 horizontal 時,layout_gravity 其實是無效的。

模型三:水平四列雙耳互補式

兩側為按鈕,中間由兩個控件互補分割:左側隨內容填充,右側自動佔滿剩餘空間,兩者寬度之和不得超過中間區域。可視為雙耳式在多列情境下的延伸。

<!--雙耳式在多列情況下的擴展式-->
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="https://schemas.android.com/apk/res/android"
 android:layout_width="fill_parent" android:layout_height="wrap_content"
 android:background="@drawable/layout01_bg"
 android:paddingLeft="10dip" android:paddingRight="10dip">
 <Button android:id="@+id/left_button"
  android:layout_alignParentLeft="true" android:layout_centerVertical="true"
  android:layout_width="wrap_content" android:layout_height="wrap_content"
  android:background="@drawable/layout01_tool" android:text="返回列表" android:padding="8dip" />
 <Button android:id="@+id/right_button"
  android:layout_alignParentRight="true" android:layout_centerVertical="true"
  android:layout_width="wrap_content" android:layout_height="wrap_content"
  android:background="@drawable/layout01_tool" android:text="評論" android:padding="8dip" />
 <!--寬度 wrap_content,隨內容加寬-->
 <TextView android:id="@+id/center_text_01"
  android:layout_toRightOf="@id/left_button" android:layout_centerVertical="true"
  android:gravity="left" android:paddingLeft="5dip"
  android:layout_width="wrap_content" android:layout_height="wrap_content"
  android:background="#aabbcc" android:text="夫婦+小三" android:textSize="18dip" android:textColor="#000000" />
 <!--寬度 fill_parent,自動填滿剩餘空間,放在最後-->
 <TextView android:id="@+id/center_text_02"
  android:layout_toRightOf="@id/center_text_01" android:layout_toLeftOf="@id/right_button"
  android:layout_centerVertical="true" android:gravity="right" android:paddingLeft="5dip"
  android:layout_width="fill_parent" android:layout_height="wrap_content"
  android:background="#ccaabb" android:text="何求" android:textSize="18dip" android:textColor="#000000" />
</RelativeLayout>

模型四:水平多列分攤式(增強版)

幾大模塊均佔整個區域,彼此以細小分割線隔離。分割線不參與分攤,模塊標題則平均分配。直接用 LinearLayout,模塊設 layout_weight="1",分割線不設權重。

<!--此代碼採用動態生成,稍加判斷即可達到相同效果-->
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="https://schemas.android.com/apk/res/android"
 android:layout_width="fill_parent" android:layout_height="25dip" android:background="#ffffff">
 <TextView android:text="首頁" android:layout_weight="1" android:gravity="center"
  android:layout_gravity="center_vertical" android:layout_width="wrap_content" android:layout_height="wrap_content" />
 <ImageView android:gravity="center" android:layout_gravity="center_vertical"
  android:layout_width="10dip" android:layout_height="wrap_content" android:src="@drawable/layout04_split" />
 <TextView android:text="資訊" android:layout_weight="1" android:gravity="center"
  android:layout_gravity="center_vertical" android:layout_width="wrap_content" android:layout_height="wrap_content" />
 <ImageView android:gravity="center" android:layout_gravity="center_vertical"
  android:layout_width="10dip" android:layout_height="wrap_content" android:src="@drawable/layout04_split" />
 <TextView android:text="博客" android:layout_weight="1" android:gravity="center"
  android:layout_gravity="center_vertical" android:layout_width="wrap_content" android:layout_height="wrap_content" />
 <ImageView android:gravity="center" android:layout_gravity="center_vertical"
  android:layout_width="10dip" android:layout_height="wrap_content" android:src="@drawable/layout04_split" />
 <TextView android:text="圖片" android:layout_weight="1" android:gravity="center"
  android:layout_gravity="center_vertical" android:layout_width="wrap_content" android:layout_height="wrap_content" />
 <ImageView android:gravity="center" android:layout_gravity="center_vertical"
  android:layout_width="10dip" android:layout_height="wrap_content" android:src="@drawable/layout04_split" />
 <TextView android:text="論壇" android:layout_weight="1" android:gravity="center"
  android:layout_gravity="center_vertical" android:layout_width="wrap_content" android:layout_height="wrap_content" />
</LinearLayout>

模型五:垂直三行天地式

類似水平雙耳式的概念,上下固定、中間自適應填滿,使用 RelativeLayout。

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="https://schemas.android.com/apk/res/android"
 android:layout_width="fill_parent" android:layout_height="fill_parent">
 <TextView android:id="@+id/top_text" android:layout_alignParentTop="true"
  android:layout_width="fill_parent" android:layout_height="wrap_content"
  android:gravity="center" android:text="上海車展" android:textColor="#ffffff" />
 <LinearLayout android:id="@+id/bottom_linear"
  android:layout_width="fill_parent" android:layout_height="wrap_content"
  android:gravity="center" android:layout_alignParentBottom="true"
  android:background="#123456" android:orientation="horizontal">
  <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="上一張" />
  <Button android:layout_width="wrap_content" android:layout_height="wrap_content"
   android:layout_gravity="center" android:text="下一張" />
 </LinearLayout>
 <!--中間主體部分,用 LinearLayout 包裹,可填充任意控件組合-->
 <LinearLayout android:id="@+id/center_linear"
  android:layout_below="@id/top_text" android:layout_above="@id/bottom_linear"
  android:layout_width="fill_parent" android:layout_height="fill_parent"
  android:background="#ffffff" android:gravity="center">
  <ImageView android:layout_width="fill_parent" android:layout_height="fill_parent" android:src="@drawable/shanhai" />
 </LinearLayout>
</RelativeLayout>

模型六:垂直三行彈簧式

結構簡單,如同一根彈簧:最底下一行能伸能屈,中間部分隨內容固定,剩餘空間交給底部填滿。

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="https://schemas.android.com/apk/res/android"
 android:layout_width="fill_parent" android:layout_height="fill_parent">
 <!--頂部-->
 <TextView android:id="@+id/top_text" android:layout_alignParentTop="true"
  android:layout_width="fill_parent" android:layout_height="wrap_content"
  android:gravity="center" android:text="上海車展" android:textColor="#ffffff" />
 <!--頂部下方為中間導航部分-->
 <LinearLayout android:id="@+id/center_linear" android:layout_below="@id/top_text"
  android:layout_width="fill_parent" android:layout_height="wrap_content"
  android:gravity="center" android:background="#123456" android:orientation="horizontal">
  <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="上一張" />
  <Button android:layout_width="wrap_content" android:layout_height="wrap_content"
   android:layout_gravity="center" android:text="下一張" />
 </LinearLayout>
 <!--最後部分填滿剩餘區域-->
 <LinearLayout android:id="@+id/bottom_linear" android:layout_below="@id/center_linear"
  android:layout_width="fill_parent" android:layout_height="fill_parent"
  android:background="#ffffff" android:gravity="center" android:layout_alignParentBottom="true">
  <ImageView android:layout_width="fill_parent" android:layout_height="fill_parent" android:src="@drawable/shanhai" />
 </LinearLayout>
</RelativeLayout>

初探之下先整理出這六種簡易模型。受手機螢幕尺寸與長寬比的限制,許多網頁佈局在手機上的實作其實相當不易。若你手上有更經典的佈局案例,歡迎留言交流、彼此分享。

最後送上一段描述佈局選擇要訣的三字文:

上中下,左中右,多行列,用相對。

線性局,緊湊排,無方向,可居中。

幀布局,定位准,相關弱,代碼少。

  1. 上一篇文章:
  2. 下一篇文章:
Copyright © Android教程網 All Rights Reserved