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

徹底搞懂 Android 的 layout_weight:從原理推導到用 ListView 做表格

編輯:Android 開發實例

這一篇要談的是 android:layout_weight 這個屬性,以及怎麼用它配合 ListView 做出一個表格樣式。

先說個前提:Android 其實提供了專門的 TableLayout 來處理表格,但我在實際專案裡幾乎沒用過它。日常開發我主要依賴 LinearLayout 與 RelativeLayout 這兩種配置,早期偏好 RelativeLayout 是因為可以直接拖曳元件排版,省去寫巢狀結構的麻煩;現在兩者大致各半。而 LinearLayout 裡最常被拿來調整整體比例的,就是 layout_weight。很多人對它的行為只有模糊印象,下面我們用幾個具體例子把它徹底算清楚。

weight 到底在分配什麼

一句話概括:layout_weight 決定的是 LinearLayout 在給子元件分完「固有大小」之後,剩下的空間要怎麼切分。看第一個例子,一個水平 LinearLayout 裡放三個 TextView:

  1. <LinearLayout xmlns:android="https://schemas.android.com/apk/res/android" 
  2.     xmlns:tools="https://schemas.android.com/tools" 
  3.     android:layout_width="match_parent" 
  4.     android:layout_height="match_parent" 
  5.     android:orientation="horizontal" > 
  6.  
  7.     <TextView 
  8.         android:layout_width="wrap_content" 
  9.         android:layout_height="wrap_content" 
  10.         android:background="#0045f5" 
  11.         android:gravity="center" 
  12.         android:text="1" /> 
  13.  
  14.     <TextView 
  15.         android:layout_width="wrap_content" 
  16.         android:layout_height="wrap_content" 
  17.         android:background="#00ff47" 
  18.         android:gravity="center" 
  19.         android:text="2"  
  20.         android:layout_weight="1"/> 
  21.  
  22.     <TextView 
  23.         android:layout_width="wrap_content" 
  24.         android:layout_height="wrap_content" 
  25.         android:background="#ff5600" 
  26.         android:gravity="center" 
  27.         android.layout_weight="1" 
  28.         android:text="3" /> 
  29.  
  30. </LinearLayout> 

三個 TextView 的寬度都是 wrap_content,也就是依內容自撐。假設 LinearLayout 先給每個 TextView 分配 10dip,螢幕寬度 480dip,那麼剩餘空間就是 480 − 3×10 = 450dip。第一個 TextView 沒設 weight,就維持 10dip;後兩個 weight 都是 1,於是把 450dip 平分,各得 225dip,最終寬度為 10 + 225 = 235dip。這就是為什麼第一個看起來特別窄,後兩個卻一樣寬。

常見誤解:wrap_content 配 weight 不等於按比例

如果元件寬度是 wrap_content,你期待它按比例瓜分空間,那結果幾乎一定不如預期。把第二個 TextView 的內容換成一長串文字再試:

  1. <LinearLayout xmlns:android="https://schemas.android.com/apk/res/android" 
  2.     xmlns:tools="https://schemas.android.com/tools" 
  3.     android:layout_width="match_parent" 
  4.     android:layout_height="match_parent" 
  5.     android:orientation="horizontal" > 
  6.  
  7.     <TextView 
  8.         android:layout_width="wrap_content" 
  9.         android:layout_height="wrap_content" 
  10.         android:background="#0045f5" 
  11.         android:gravity="center" 
  12.         android:text="1" /> 
  13.  
  14.     <TextView 
  15.         android:layout_width="wrap_content" 
  16.         android:layout_height="wrap_content" 
  17.         android.background="#00ff47" 
  18.         android:gravity="center" 
  19.         android.text="2222222222222222222" 
  20.         android.layout_weight="1"/> 
  21.  
  22.     <TextView 
  23.         android:layout_width="wrap_content" 
  24.         android.layout_height="wrap_content" 
  25.         android.background="#ff5600" 
  26.         android:gravity="center" 
  27.         android.layout_weight="1" 
  28.         android.text="3" /> 
  29. </LinearLayout> 

原理其實和上一段完全一樣,只是這次 LinearLayout 先按內容給了第二個 TextView 一個很寬的初始值,剩餘空間被壓縮,於是「後兩個平分剩餘空間」這個前提就已經失真了。內容越長,它自己先吃掉的比例越大。

正解:把寬度設成 0dip

知道了原理,解法就很單純——讓「固有大小」歸零,全部空間都交給權重去分。想做出 1:2:3 的效果:

  1. <LinearLayout xmlns:android="https://schemas.android.com/apk/res/android" 
  2.     xmlns:tools="https://schemas.android.com/tools" 
  3.     android:layout_width="match_parent" 
  4.     android:layout_height="match_parent" 
  5.     android.orientation="horizontal" > 
  6.  
  7.     <TextView 
  8.         android.layout_width="0dip" 
  9.         android.layout_height="wrap_content" 
  10.         android.background="#0045f5" 
  11.         android.gravity="center" 
  12.         android.layout_weight="1" 
  13.         android.text="1" /> 
  14.  
  15.     <TextView 
  16.         android.layout_width="0dip" 
  17.         android.layout_height="wrap_content" 
  18.         android.background="#00ff47" 
  19.         android.gravity="center" 
  20.         android.text="2222222222222222222" 
  21.         android.layout_weight="2"/> 
  22.  
  23.     <TextView 
  24.         android.layout_width="0dip" 
  25.         android.layout_height="wrap_content" 
  26.         android.background="#ff5600" 
  27.         android.gravity="center" 
  28.         android.layout_weight="3" 
  29.         android.text="3" /> 
  30. </LinearLayout> 

此時初始分配為 0,剩餘空間就是完整的 480dip,再按權重切成 1:2:3。這也是表格類版面最該用的寫法。

反直覺的情況:fill_parent 配 weight

看到這裡你可能會下結論「權重越大佔的空間越大」。先別急,再看一個例子:把寬度改成 fill_parent,權重分別給 1、2、2。

  1. <LinearLayout xmlns:android="https://schemas.android.com/apk/res/android" 
  2.     xmlns:tools="https://schemas.android.com/tools" 
  3.     android:layout_width="match_parent" 
  4.     android:layout_height="match_parent" 
  5.     android:orientation="horizontal" > 
  6.  
  7.     <TextView 
  8.         android.layout_width="fill_parent" 
  9.         android.layout_height="wrap_content" 
  10.         android.background="#0045f5" 
  11.         android.gravity="center" 
  12.         android.layout_weight="1" 
  13.         android.text="1" /> 
  14.  
  15.     <TextView 
  16.         android.layout_width="fill_parent" 
  17.         android.layout_height="wrap_content" 
  18.         android.background="#00ff47" 
  19.         android.gravity="center" 
  20.         android.text="2" 
  21.         android.layout_weight="2"/> 
  22.  
  23.     <TextView 
  24.         android.layout_width="fill_parent" 
  25.         android.layout_height="wrap_content" 
  26.         android.background="#ff5600" 
  27.         android.gravity="center" 
  28.         android.layout_weight="2" 
  29.         android.text="3" /> 
  30. </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,實際比例就得自己動腦筋算了。

實作:用 ListView 拼出表格

有了這個結論,做表格就變得很輕鬆。頁面結構是一個垂直 LinearLayout,上方 include 表格標題列,下方接一個 ListView:

  1. <LinearLayout xmlns:android="https://schemas.android.com/apk/res/android" 
  2.     xmlns:tools="https://schemas.android.com/tools" 
  3.     android:orientation="vertical" 
  4.     android.layout_margin="10dip" 
  5.     android.layout_width="match_parent" 
  6.     android.layout_height="match_parent" > 
  7.     
  8.     <include 
  9.         layout="@layout/list_item" 
  10.         android:id="@+id/table_title"/> 
  11.  
  12.     <ListView 
  13.         android:id="@+id/list" 
  14.         android:divider="#f9b68b" 
  15.         android:dividerHeight="1.0dip" 
  16.         android:scrollbars="none" 
  17.         android:background="@drawable/listview_bg" 
  18.         android:cacheColorHint="@android:color/transparent" 
  19.         android:fadingEdge="none" 
  20.         android:layout_width="fill_parent" 
  21.         android.layout_height="wrap_content" > 
  22.     </ListView> 
  23.  
  24. </LinearLayout> 

標題列與每一列的內容共用同一個 item 版面,這麼做的好處是欄寬天然對齊,不必另外算座標。三個 TextView 寬度都是 0dip,中間用兩個細長的 View 充當分隔線,權重比 2:1:1:

  1. <?xml version="1.0" encoding="UTF-8"?> 
  2. <LinearLayout xmlns:android="https://schemas.android.com/apk/res/android" 
  3.     android:orientation="horizontal" 
  4.     android:layout_width="fill_parent" 
  5.     android.layout_height="wrap_content" > 
  6.  
  7.     <TextView 
  8.         android:id="@+id/text_name" 
  9.         android.layout_width="0dip" 
  10.         android.layout_height="wrap_content" 
  11.         android.layout_weight="2" 
  12.         android.gravity="center" 
  13.         android.paddingBottom="10dip" 
  14.         android.paddingTop="10dip" 
  15.         android.text="姓名" /> 
  16.  
  17.     <View 
  18.         android.layout_width="1.5dip" 
  19.         android.layout_height="fill_parent" 
  20.         android.background="#f9b68b"/> 
  21.  
  22.     <TextView 
  23.         android:id="@+id/text_sex" 
  24.         android.layout_width="0dip" 
  25.         android.layout_height="wrap_content" 
  26.         android.layout_weight="1" 
  27.         android.paddingBottom="10dip" 
  28.         android.paddingTop="10dip" 
  29.         android.gravity="center" 
  30.         android.text="性別" /> 
  31.  
  32.     <View 
  33.         android.layout_width="1.5dip" 
  34.         android.layout_height="fill_parent" 
  35.         android.background="#f9b68b"/> 
  36.  
  37.     <TextView 
  38.         android:id="@+id/text_age" 
  39.         android.layout_width="0dip" 
  40.         android.layout_height="wrap_content" 
  41.         android.layout_weight="1" 
  42.         android.paddingBottom="10dip" 
  43.         android.paddingTop="10dip" 
  44.         android.gravity="center" 
  45.         android.text="年齡" /> 
  46.  
  47. </LinearLayout> 

因為寬度是 0dip,欄位不會被內容長度擠變形,這一點在資料格式不固定時尤其重要。

資料物件很單純,就是一個有三個欄位的 POJO:

  1. package com.example.listviewtable; 
  2.  
  3. public class Person { 
  4.     private String name; 
  5.     private String sex; 
  6.     private int age; 
  7.  
  8.     public Person(String name, String sex, int age) { 
  9.         this.name = name; 
  10.         this.sex = sex; 
  11.         this.age = age; 
  12.     } 
  13.     public String getName() { return name; } 
  14.     public String getSex() { return sex; } 
  15.     public int getAge() { return age; } 
  16. }

自訂的 BaseAdapter 採用 ViewHolder 模式,避免每捲動一次就重複 findViewById:

  1. package com.example.listviewtable; 
  2.  
  3. import java.util.List; 
  4. import android.content.Context; 
  5. import android.view.LayoutInflater; 
  6. import android.view.View; 
  7. import android.view.ViewGroup; 
  8. import android.widget.BaseAdapter; 
  9. import android.widget.TextView; 
  10.  
  11. public class TableAdapter extends BaseAdapter { 
  12.     private List<Person> list; 
  13.     private LayoutInflater inflater; 
  14.  
  15.     public TableAdapter(Context context, List<Person> list){ 
  16.         this.list = list; 
  17.         inflater = LayoutInflater.from(context); 
  18.     } 
  19.  
  20.     public int getCount() { return list.size(); } 
  21.     public Object getItem(int position) { return list.get(position); } 
  22.     public long getItemId(int position) { return position; } 
  23.  
  24.     public View getView(int position, View convertView, ViewGroup parent) { 
  25.         Person person = (Person) this.getItem(position); 
  26.         ViewHolder viewHolder; 
  27.         if(convertView == null){ 
  28.             viewHolder = new ViewHolder(); 
  29.             convertView = inflater.inflate(R.layout.list_item, null); 
  30.             viewHolder.mTextName = (TextView) convertView.findViewById(R.id.text_name); 
  31.             viewHolder.mTextSex = (TextView) convertView.findViewById(R.id.text_sex); 
  32.             viewHolder.mTextAge = (TextView) convertView.findViewById(R.id.text_age); 
  33.             convertView.setTag(viewHolder); 
  34.         }else{ 
  35.             viewHolder = (ViewHolder) convertView.getTag(); 
  36.         } 
  37.  
  38.         viewHolder.mTextName.setText(person.getName()); 
  39.         viewHolder.mTextSex.setText(person.getSex()); 
  40.         viewHolder.mTextAge.setText(person.getAge() + "歲"); 
  41.         return convertView; 
  42.     } 
  43.  
  44.     public static class ViewHolder{ 
  45.         public TextView mTextName; 
  46.         public TextView mTextSex; 
  47.         public TextView mTextAge; 
  48.     } 
  49. }

最後是 Activity,塞一批假資料進去並掛上 adapter,順便把標題列的底色改成橘色以區隔資料列:

  1. package com.example.listviewtable; 
  2.  
  3. import java.util.ArrayList; 
  4. import java.util.List; 
  5. import android.app.Activity; 
  6. import android.graphics.Color; 
  7. import android.os.Bundle; 
  8. import android.view.ViewGroup; 
  9. import android.widget.ListView; 
  10.  
  11. public class ListTableActivity extends Activity { 
  12.     protected void onCreate(Bundle savedInstanceState) { 
  13.         super.onCreate(savedInstanceState); 
  14.         setContentView(R.layout.activity_main); 
  15.  
  16.         //設置表格標題的背景顏色 
  17.         ViewGroup tableTitle = (ViewGroup) findViewById(R.id.table_title); 
  18.         tableTitle.setBackgroundColor(Color.rgb(255, 100, 10)); 
  19.  
  20.         List<Person> list = new ArrayList<Person>(); 
  21.         for(int i = 0; i < 10; i++){ 
  22.             list.add(new Person("劉德華", "男", 50)); 
  23.         } 
  24.  
  25.         ListView tableListView = (ListView) findViewById(R.id.list); 
  26.         tableListView.setAdapter(new TableAdapter(this, list)); 
  27.     } 
  28. }

整份實作沒有寫一行自訂的繪製程式碼,全靠 layout_weight 把欄寬綁成固定比例,表格就成形了。理解這個屬性的關鍵只有一句:它分配的是「扣掉初始尺寸之後的剩餘量」,而初始尺寸取決於你把 width/height 設成什麼。

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