編輯:Android 開發實例
在 Android 中實作底部佈局,往往會使用 RelativeLayout 並設定 android:layout_alignParentBottom="true",這樣很容易就能把某個元件固定在底部。然而對於較複雜的佈局,單靠這個屬性無法達到理想效果。例如 top、center、bottom 三層結構,很可能因為中間層(center)資料過多,導致無法完整顯示,甚至把 bottom 層擠出畫面。
解決之道是:在採用 RelativeLayout 時,除了為底部層設定 android:layout_alignParentBottom="true" 外,還要為中間層加上 android:layout_above="@id/bottom" 與 android:layout_below="@id/top"。這樣既能確保 center 層處於中間位置,又能自適應顯示滾動條。
以下範例即實作這種三層底部佈局。圖 1 為三層底部佈局介面,圖 2 為彈出輸入法時仍顯示的底部按鈕。

圖-1 三層的底部佈局介面

圖 2 彈出輸入法時顯示的底部按鈕
此專案主要示範資料填充與佈局,故僅做簡單的資料載入:
package com.BottomTest.main;
import java.util.ArrayList;
import java.util.HashMap;
import android.app.Activity;
import android.os.Bundle;
import android.widget.ListView;
import android.widget.SimpleAdapter;
public class BottomTestActivity extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
ListView list = (ListView) findViewById(R.id.friends);
//存儲數據的數組列表
ArrayList<HashMap<String, Object>> listData=new ArrayList<HashMap<String,Object>>();
String []name={"William","Charles","Linng","Json","Bob","Carli"};
String []id={"12","16","33","21","34","22"};
for(int i=0;i<6;i++){
HashMap<String, Object> map=new HashMap<String, Object>();
map.put("friend_image", R.drawable.icon);
map.put("friend_username", name[i]);
map.put("friend_id", id[i]);
listData.add(map);
}
//適配器
SimpleAdapter listItemAdapter=new SimpleAdapter(this, listData, R.layout.item,
new String[] {"friend_image","friend_username","friend_id" },
new int[] { R.id.friend_image, R.id.friend_username, R.id.friend_id });
list.setAdapter(listItemAdapter);
}
}
<?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="wrap_content"
android:orientation="vertical">
<RelativeLayout android:id="@+id/bottom"
android:layout_width="fill_parent"
android:layout_height="wrap_content" >
<LinearLayout android:id="@+id/top"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<EditText android:id="@+id/view_user_input"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginTop="6dip"
android:layout_marginLeft="12dip"
android:singleLine="true"
android:numeric="integer"
android:imeOptions="actionDone"
android:hint="輸入用戶ID"
android:layout_weight="1"/>
<Button android:id="@+id/view_user"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginTop="4dip"
android:layout_weight="3"
android:text="查看"/>
</LinearLayout>
<LinearLayout android:id="@+id/center"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:layout_above="@id/bottom"
android:layout_below="@id/top">
<TextView android:id="@+id/my_friends_list"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="好友列表"
android:paddingTop="6dip"
android:paddingLeft="2dip"
android:layout_marginLeft="10dip"/>
<ListView android:id="@+id/friends"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="6dip"/>
</LinearLayout>
<LinearLayout android:id="@+id/bottom"
android:background="@drawable/bg"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_alignParentBottom="true" >
<Button android:id="@+id/refresh"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginTop="2dip"
android:text="刷新用戶列表"
android:layout_weight="1"/>
<Button android:id="@+id/back"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginTop="2dip"
android:text="返回"
android:layout_weight="1"/>
</LinearLayout>
</RelativeLayout>
</LinearLayout>
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="https://schemas.android.com/apk/res/android"
android:id="@+id/RelativeLayout"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:paddingBottom="4dip"
android:paddingRight="12dip">
<ImageView android:id="@+id/friend_image"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingTop="6dip"
android:paddingLeft="2dip"
android:layout_centerVertical="true"
android:layout_alignParentLeft="true"/>
<TextView android:id="@+id/friend_username"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textSize="18dip"
android:textColor="#ccc"
android:paddingTop="6dip"
android:paddingRight="2dip"
android:layout_toRightOf="@id/friend_image" />
<TextView android:id="@+id/friend_id"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/friend_username"
android:layout_marginRight="36dip"
android:paddingRight="2dip"
android:layout_toRightOf="@id/friend_image"
android:textColor="#fff"
android:maxLines="2"/>
</RelativeLayout>
透過上述佈局設定,就能在 ListView 列表中讓底部按鈕始終固定於畫面最下方,同時兼顧中間列表內容過多時的滾動顯示。
Android JSON 解析教學:用 JSONObject 擷取所需資料
JSON 是 JavaScript Object Notation 的縮寫,它是一種與語言無關的資料交換格式,也是 XML 廣受歡迎的替代方案。本章將說明如何在 Andr
Android 如何解析 JSON?從 JSONObject 到天氣查詢 App 的完整示範
行動裝置上的 App 要拿到伺服器的資料,回傳格式十之八九是 JSON。JSON 的全名是 JavaScript Object Notation,它與程式語言無關、純文字
Android MediaPlayer 多媒體播放教學
Android 提供了多種控制音訊與影片播放的方式,無論是播放本機檔案或串流媒體都能勝任,而其中最常被使用的,莫過於 MediaPlayer 這個類別。 Android
文字繞圖排版實作:讓文字環繞圖片並自然接續
做內容型 App 時常會遇到這種版面:一張圖片靠左(或靠右),文字繞著它排;圖片下方的文字還要跟右側的文字連貫起來,讀起來像同一段話。這件事 TextView 本身做不到