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

ListView 中讓 Button 始終固定在底部的範例

編輯: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 彈出輸入法時顯示的底部按鈕

Activity 程式碼

此專案主要示範資料填充與佈局,故僅做簡單的資料載入:

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);
}
}

主要佈局檔 main.xml

<?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>

ListView 項目佈局檔 item.xml

<?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 列表中讓底部按鈕始終固定於畫面最下方,同時兼顧中間列表內容過多時的滾動顯示。

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