編輯: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>
初探之下先整理出這六種簡易模型。受手機螢幕尺寸與長寬比的限制,許多網頁佈局在手機上的實作其實相當不易。若你手上有更經典的佈局案例,歡迎留言交流、彼此分享。
最後送上一段描述佈局選擇要訣的三字文:
上中下,左中右,多行列,用相對。
線性局,緊湊排,無方向,可居中。
幀布局,定位准,相關弱,代碼少。
Android 中的 TCP 與 UDP 傳輸實作
TCP 與 UDP 是 Android 網路開發中常見的傳輸協定。TCP 面向連接、可靠且按序傳輸;UDP 無連接、低延遲,適合理會即時性較高的場景。TCP 通訊概念TC
Android 實戰:手機短信驗證碼獲取與注冊全流程解析
注冊各類 App 或網站賬號時,手機短信驗證碼幾乎是標配環節。曾經只覺得「手機收驗證碼」這件事挺新奇,卻沒深究背後是怎麼實現的。如今手上的項目正好需要通過手機號注冊、並發
Android 如何解析 JSON:從 API 回傳資料中取出欄位值
JSON 的全名是 JavaScript Object Notation,是一種與程式語言無關的資料交換格式。相較於 XML,它沒有冗長的標籤,結構清楚、體積更小,解析速
Android 載入動畫怎麼做?ProgressBar 轉圈指示器完整說明
使用者按下一顆按鈕後若畫面毫無反應,通常會以為 App 當掉了。這時候一個小小的旋轉動畫,就能把「不確定要等多久」的焦慮大幅降低。Android 的 ProgressBa