android中listView下有Button按钮始终在最底部

http://www.yoyong.com/archives/436

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 弹出输入法时显示的底部按钮

项目只是实现主要的数据填充及布局,故只是简单的文件加载。以下是源码:

1.BottomTestActivity.java

  1. package com.BottomTest.main;  
  2.   
  3. import java.util.ArrayList;  
  4. import java.util.HashMap;  
  5.   
  6. import android.app.Activity;  
  7. import android.os.Bundle;  
  8. import android.widget.ListView;  
  9. import android.widget.SimpleAdapter;  
  10.   
  11. public class BottomTestActivity extends Activity {  
  12.   
  13. /** Called when the activity is first created. */  
  14. @Override  
  15. public void onCreate(Bundle savedInstanceState) {  
  16. super.onCreate(savedInstanceState);  
  17. setContentView(R.layout.main);  
  18. ListView list = (ListView) findViewById(R.id.friends);  
  19. //存储数据的数组列表  
  20. ArrayList<HashMap<String, Object>> listData=new ArrayList<HashMap<String,Object>>();  
  21. String []name={"William","Charles","Linng","Json","Bob","Carli"};  
  22. String []id={"12","16","33","21","34","22"};  
  23. for(int i=0;i<6;i++){  
  24. HashMap<String, Object> map=new HashMap<String, Object>();  
  25. map.put("friend_image", R.drawable.icon);  
  26. map.put("friend_username", name[i]);  
  27. map.put("friend_id", id[i]);  
  28. listData.add(map);  
  29. }  
  30. //适配器  
  31. SimpleAdapter listItemAdapter= new SimpleAdapter(this,  
  32. listData,  
  33. R.layout.item,  
  34. new String[] { "friend_image""friend_username""friend_id" },  
  35. new int[] { R.id.friend_image, R.id.friend_username, R.id.friend_id });  
  36. list.setAdapter(listItemAdapter);  
  37. }  
  38. }  

主要布局文件

2.main.xml

  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <LinearLayout  
  3. xmlns:android="http://schemas.android.com/apk/res/android"  
  4. android:layout_width="fill_parent"  
  5. android:layout_height="wrap_content"  
  6. android:orientation="vertical">  
  7. <RelativeLayout android:id="@+id/bottom"  
  8. android:layout_width="fill_parent"  
  9. android:layout_height="wrap_content"   >  
  10. <LinearLayout android:id="@+id/top"  
  11. android:layout_width="fill_parent"  
  12. android:layout_height="wrap_content"  
  13. android:orientation="horizontal"  >  
  14. <EditText android:id="@+id/view_user_input"  
  15. android:layout_width="fill_parent"  
  16. android:layout_height="wrap_content"  
  17. android:layout_marginTop="6dip"  
  18. android:layout_marginLeft="12dip"  
  19. android:singleLine="true"  
  20. android:numeric="integer"  
  21. android:imeOptions="actionDone"  
  22. android:hint="输入用户ID"  
  23. android:layout_weight="1"/>  
  24. <Button android:id="@+id/view_user"  
  25. android:layout_width="fill_parent"  
  26. android:layout_height="wrap_content"  
  27. android:layout_marginTop="4dip"  
  28. android:layout_weight="3"  
  29. android:text="查看"/>  
  30. </LinearLayout>  
  31. <LinearLayout android:id="@+id/center"  
  32. android:layout_width="fill_parent"  
  33. android:layout_height="wrap_content"  
  34. android:orientation="vertical"  
  35. android:layout_above="@id/bottom"  
  36. android:layout_below="@id/top" >  
  37. <TextView android:id="@+id/my_friends_list"  
  38. android:layout_width="fill_parent"  
  39. android:layout_height="wrap_content"  
  40. android:text="好友列表"  
  41. android:paddingTop="6dip"  
  42. android:paddingLeft="2dip"  
  43. android:layout_marginLeft="10dip"/>  
  44. <ListView android:id="@+id/friends"  
  45. android:layout_width="fill_parent"  
  46. android:layout_height="wrap_content"  
  47. android:layout_marginBottom="6dip"/>  
  48. </LinearLayout>  
  49. <LinearLayout android:id="@+id/bottom"  
  50. android:background="@drawable/bg"  
  51. android:layout_width="fill_parent"  
  52. android:layout_height="wrap_content"  
  53. android:orientation="horizontal"  
  54. android:layout_alignParentBottom="true"  >  
  55. <Button android:id="@+id/refresh"  
  56. android:layout_width="fill_parent"  
  57. android:layout_height="wrap_content"  
  58. android:layout_marginTop="2dip"  
  59. android:text="刷新用户列表"  
  60. android:layout_weight="1"/>  
  61. <Button android:id="@+id/back"  
  62. android:layout_width="fill_parent"  
  63. android:layout_height="wrap_content"  
  64. android:layout_marginTop="2dip"  
  65. android:text="返回"  
  66. android:layout_weight="1"/>  
  67. </LinearLayout>  
  68. </RelativeLayout>  
  69. </LinearLayout>  

listview item内容的布局文件

3.item.xml

  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  3. android:id="@+id/RelativeLayout"  
  4. android:layout_width="fill_parent"  
  5. android:layout_height="wrap_content"  
  6. android:paddingBottom="4dip"  
  7. android:paddingRight="12dip" >  
  8. <ImageView android:id="@+id/friend_image"  
  9. android:layout_width="wrap_content"  
  10. android:layout_height="wrap_content"  
  11. android:paddingTop="6dip"  
  12. android:paddingLeft="2dip"  
  13. android:layout_centerVertical="true"  
  14. android:layout_alignParentLeft="true" />  
  15. <TextView android:id="@+id/friend_username"  
  16. android:layout_width="fill_parent"  
  17. android:layout_height="wrap_content"  
  18. android:textSize="18dip"  
  19. android:textColor="#ccc"  
  20. android:paddingTop="6dip"  
  21. android:paddingRight="2dip"  
  22. android:layout_toRightOf="@id/friend_image"    />  
  23. <TextView android:id="@+id/friend_id"  
  24. android:layout_width="fill_parent"  
  25. android:layout_height="wrap_content"  
  26. android:layout_below="@+id/friend_username"  
  27. android:layout_marginRight="36dip"  
  28. android:paddingRight="2dip"  
  29. android:layout_toRightOf="@id/friend_image"  
  30. android:textColor="#fff"  
  31. android:maxLines="2" />  
  32. </RelativeLayout>  

评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值