編輯:Android 開發實例
這一篇要談的是 android:layout_weight 這個屬性,以及怎麼用它配合 ListView 做出一個表格樣式。
先說個前提:Android 其實提供了專門的 TableLayout 來處理表格,但我在實際專案裡幾乎沒用過它。日常開發我主要依賴 LinearLayout 與 RelativeLayout 這兩種配置,早期偏好 RelativeLayout 是因為可以直接拖曳元件排版,省去寫巢狀結構的麻煩;現在兩者大致各半。而 LinearLayout 裡最常被拿來調整整體比例的,就是 layout_weight。很多人對它的行為只有模糊印象,下面我們用幾個具體例子把它徹底算清楚。
一句話概括:layout_weight 決定的是 LinearLayout 在給子元件分完「固有大小」之後,剩下的空間要怎麼切分。看第一個例子,一個水平 LinearLayout 裡放三個 TextView:
- <LinearLayout xmlns:android="https://schemas.android.com/apk/res/android"
- xmlns:tools="https://schemas.android.com/tools"
- android:layout_width="match_parent"
- android:layout_height="match_parent"
- android:orientation="horizontal" >
- <TextView
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:background="#0045f5"
- android:gravity="center"
- android:text="1" />
- <TextView
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:background="#00ff47"
- android:gravity="center"
- android:text="2"
- android:layout_weight="1"/>
- <TextView
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:background="#ff5600"
- android:gravity="center"
- android.layout_weight="1"
- android:text="3" />
- </LinearLayout>
三個 TextView 的寬度都是 wrap_content,也就是依內容自撐。假設 LinearLayout 先給每個 TextView 分配 10dip,螢幕寬度 480dip,那麼剩餘空間就是 480 − 3×10 = 450dip。第一個 TextView 沒設 weight,就維持 10dip;後兩個 weight 都是 1,於是把 450dip 平分,各得 225dip,最終寬度為 10 + 225 = 235dip。這就是為什麼第一個看起來特別窄,後兩個卻一樣寬。
如果元件寬度是 wrap_content,你期待它按比例瓜分空間,那結果幾乎一定不如預期。把第二個 TextView 的內容換成一長串文字再試:
- <LinearLayout xmlns:android="https://schemas.android.com/apk/res/android"
- xmlns:tools="https://schemas.android.com/tools"
- android:layout_width="match_parent"
- android:layout_height="match_parent"
- android:orientation="horizontal" >
- <TextView
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:background="#0045f5"
- android:gravity="center"
- android:text="1" />
- <TextView
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android.background="#00ff47"
- android:gravity="center"
- android.text="2222222222222222222"
- android.layout_weight="1"/>
- <TextView
- android:layout_width="wrap_content"
- android.layout_height="wrap_content"
- android.background="#ff5600"
- android:gravity="center"
- android.layout_weight="1"
- android.text="3" />
- </LinearLayout>
原理其實和上一段完全一樣,只是這次 LinearLayout 先按內容給了第二個 TextView 一個很寬的初始值,剩餘空間被壓縮,於是「後兩個平分剩餘空間」這個前提就已經失真了。內容越長,它自己先吃掉的比例越大。
知道了原理,解法就很單純——讓「固有大小」歸零,全部空間都交給權重去分。想做出 1:2:3 的效果:
- <LinearLayout xmlns:android="https://schemas.android.com/apk/res/android"
- xmlns:tools="https://schemas.android.com/tools"
- android:layout_width="match_parent"
- android:layout_height="match_parent"
- android.orientation="horizontal" >
- <TextView
- android.layout_width="0dip"
- android.layout_height="wrap_content"
- android.background="#0045f5"
- android.gravity="center"
- android.layout_weight="1"
- android.text="1" />
- <TextView
- android.layout_width="0dip"
- android.layout_height="wrap_content"
- android.background="#00ff47"
- android.gravity="center"
- android.text="2222222222222222222"
- android.layout_weight="2"/>
- <TextView
- android.layout_width="0dip"
- android.layout_height="wrap_content"
- android.background="#ff5600"
- android.gravity="center"
- android.layout_weight="3"
- android.text="3" />
- </LinearLayout>
此時初始分配為 0,剩餘空間就是完整的 480dip,再按權重切成 1:2:3。這也是表格類版面最該用的寫法。
看到這裡你可能會下結論「權重越大佔的空間越大」。先別急,再看一個例子:把寬度改成 fill_parent,權重分別給 1、2、2。
- <LinearLayout xmlns:android="https://schemas.android.com/apk/res/android"
- xmlns:tools="https://schemas.android.com/tools"
- android:layout_width="match_parent"
- android:layout_height="match_parent"
- android:orientation="horizontal" >
- <TextView
- android.layout_width="fill_parent"
- android.layout_height="wrap_content"
- android.background="#0045f5"
- android.gravity="center"
- android.layout_weight="1"
- android.text="1" />
- <TextView
- android.layout_width="fill_parent"
- android.layout_height="wrap_content"
- android.background="#00ff47"
- android.gravity="center"
- android.text="2"
- android.layout_weight="2"/>
- <TextView
- android.layout_width="fill_parent"
- android.layout_height="wrap_content"
- android.background="#ff5600"
- android.gravity="center"
- android.layout_weight="2"
- android.text="3" />
- </LinearLayout>
結果不是 1:2:2。原因一樣在初始分配:每個 TextView 都先被判定為「要佔滿整個 LinearLayout」,也就是各拿 480dip,剩餘空間變成 480 − 3×480 = −960dip,是個負值。接著再按權重分這個負數:第一個得到 480 + (−960)×(1/5) = 288dip,後兩個各得 480 + (−960)×(2/5) = 96dip,實際比例是 3:1:1——權重最大的反而最窄。
想讓元件嚴格按權重比例分配,寬度(或高度)必須設為 0dip;一旦設成 fill_parent/match_parent,實際比例就得自己動腦筋算了。
有了這個結論,做表格就變得很輕鬆。頁面結構是一個垂直 LinearLayout,上方 include 表格標題列,下方接一個 ListView:
- <LinearLayout xmlns:android="https://schemas.android.com/apk/res/android"
- xmlns:tools="https://schemas.android.com/tools"
- android:orientation="vertical"
- android.layout_margin="10dip"
- android.layout_width="match_parent"
- android.layout_height="match_parent" >
- <include
- layout="@layout/list_item"
- android:id="@+id/table_title"/>
- <ListView
- android:id="@+id/list"
- android:divider="#f9b68b"
- android:dividerHeight="1.0dip"
- android:scrollbars="none"
- android:background="@drawable/listview_bg"
- android:cacheColorHint="@android:color/transparent"
- android:fadingEdge="none"
- android:layout_width="fill_parent"
- android.layout_height="wrap_content" >
- </ListView>
- </LinearLayout>
標題列與每一列的內容共用同一個 item 版面,這麼做的好處是欄寬天然對齊,不必另外算座標。三個 TextView 寬度都是 0dip,中間用兩個細長的 View 充當分隔線,權重比 2:1:1:
- <?xml version="1.0" encoding="UTF-8"?>
- <LinearLayout xmlns:android="https://schemas.android.com/apk/res/android"
- android:orientation="horizontal"
- android:layout_width="fill_parent"
- android.layout_height="wrap_content" >
- <TextView
- android:id="@+id/text_name"
- android.layout_width="0dip"
- android.layout_height="wrap_content"
- android.layout_weight="2"
- android.gravity="center"
- android.paddingBottom="10dip"
- android.paddingTop="10dip"
- android.text="姓名" />
- <View
- android.layout_width="1.5dip"
- android.layout_height="fill_parent"
- android.background="#f9b68b"/>
- <TextView
- android:id="@+id/text_sex"
- android.layout_width="0dip"
- android.layout_height="wrap_content"
- android.layout_weight="1"
- android.paddingBottom="10dip"
- android.paddingTop="10dip"
- android.gravity="center"
- android.text="性別" />
- <View
- android.layout_width="1.5dip"
- android.layout_height="fill_parent"
- android.background="#f9b68b"/>
- <TextView
- android:id="@+id/text_age"
- android.layout_width="0dip"
- android.layout_height="wrap_content"
- android.layout_weight="1"
- android.paddingBottom="10dip"
- android.paddingTop="10dip"
- android.gravity="center"
- android.text="年齡" />
- </LinearLayout>
因為寬度是 0dip,欄位不會被內容長度擠變形,這一點在資料格式不固定時尤其重要。
資料物件很單純,就是一個有三個欄位的 POJO:
- package com.example.listviewtable;
- public class Person {
- private String name;
- private String sex;
- private int age;
- public Person(String name, String sex, int age) {
- this.name = name;
- this.sex = sex;
- this.age = age;
- }
- public String getName() { return name; }
- public String getSex() { return sex; }
- public int getAge() { return age; }
- }
自訂的 BaseAdapter 採用 ViewHolder 模式,避免每捲動一次就重複 findViewById:
- package com.example.listviewtable;
- import java.util.List;
- import android.content.Context;
- import android.view.LayoutInflater;
- import android.view.View;
- import android.view.ViewGroup;
- import android.widget.BaseAdapter;
- import android.widget.TextView;
- public class TableAdapter extends BaseAdapter {
- private List<Person> list;
- private LayoutInflater inflater;
- public TableAdapter(Context context, List<Person> list){
- this.list = list;
- inflater = LayoutInflater.from(context);
- }
- public int getCount() { return list.size(); }
- public Object getItem(int position) { return list.get(position); }
- public long getItemId(int position) { return position; }
- public View getView(int position, View convertView, ViewGroup parent) {
- Person person = (Person) this.getItem(position);
- ViewHolder viewHolder;
- if(convertView == null){
- viewHolder = new ViewHolder();
- convertView = inflater.inflate(R.layout.list_item, null);
- viewHolder.mTextName = (TextView) convertView.findViewById(R.id.text_name);
- viewHolder.mTextSex = (TextView) convertView.findViewById(R.id.text_sex);
- viewHolder.mTextAge = (TextView) convertView.findViewById(R.id.text_age);
- convertView.setTag(viewHolder);
- }else{
- viewHolder = (ViewHolder) convertView.getTag();
- }
- viewHolder.mTextName.setText(person.getName());
- viewHolder.mTextSex.setText(person.getSex());
- viewHolder.mTextAge.setText(person.getAge() + "歲");
- return convertView;
- }
- public static class ViewHolder{
- public TextView mTextName;
- public TextView mTextSex;
- public TextView mTextAge;
- }
- }
最後是 Activity,塞一批假資料進去並掛上 adapter,順便把標題列的底色改成橘色以區隔資料列:
- package com.example.listviewtable;
- import java.util.ArrayList;
- import java.util.List;
- import android.app.Activity;
- import android.graphics.Color;
- import android.os.Bundle;
- import android.view.ViewGroup;
- import android.widget.ListView;
- public class ListTableActivity extends Activity {
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_main);
- //設置表格標題的背景顏色
- ViewGroup tableTitle = (ViewGroup) findViewById(R.id.table_title);
- tableTitle.setBackgroundColor(Color.rgb(255, 100, 10));
- List<Person> list = new ArrayList<Person>();
- for(int i = 0; i < 10; i++){
- list.add(new Person("劉德華", "男", 50));
- }
- ListView tableListView = (ListView) findViewById(R.id.list);
- tableListView.setAdapter(new TableAdapter(this, list));
- }
- }
整份實作沒有寫一行自訂的繪製程式碼,全靠 layout_weight 把欄寬綁成固定比例,表格就成形了。理解這個屬性的關鍵只有一句:它分配的是「扣掉初始尺寸之後的剩餘量」,而初始尺寸取決於你把 width/height 設成什麼。
AsyncTask 與 Handler 機制:Android 多執行緒的兩條路
Android 的介面操作有兩條鐵則:不要阻塞 UI 執行緒,以及只在 UI 執行緒存取 UI 元件。問題在於,當你要跑一段耗時運算並把結果顯示出來時,這兩條規則天生互相
Android AIDL 跨進程服務完整實作:從服務端到客戶端全流程
在 Android 裡有所謂的 Service,但要實現「跨應用程式共享資料」時,單純的 Service 是不夠的,這時候就需要 Android Interface De
以百度地圖 SDK 實現 Android 定位功能:完整流程解析
LBS 位置服務是 Android 應用中愈來愈常見的能力,從定位、地圖顯示到導航,都建立在它之上。本文以百度地圖定位 SDK 為例,逐步示範如何完成一次定位並把結果回傳
用 ActivityGroup 與 GridView 打造 Tab 分頁框架
翻開任何一款 Android 瀏覽器或客戶端軟體,幾乎都是 Tab 分頁的界面框架。很多人直覺會想到 TabHost 加 TabActivity 的組合,但在實際專案中最