android实现底部按钮布局

来源:互联网 发布:数据挖掘 韩家炜 ppt 编辑:程序博客网 时间:2024/05/16 00:45
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. import java.util.ArrayList;
  3. import java.util.HashMap;

  4. import android.app.Activity;
  5. import android.os.Bundle;
  6. import android.widget.ListView;
  7. import android.widget.SimpleAdapter;

  8. public class BottomTestActivity extends Activity {

  9. /** Called when the activity is first created. */
  10. @Override
  11. public void onCreate(Bundle savedInstanceState) {
  12. super.onCreate(savedInstanceState);
  13. setContentView(R.layout.main);
  14. ListView list = (ListView) findViewById(R.id.friends);
  15. //存储数据的数组列表
  16. ArrayList<HashMap<String, Object>> listData=new ArrayList<HashMap<String,Object>>();
  17. String []name={"William","Charles","Linng","Json","Bob","Carli"};
  18. String []id={"12","16","33","21","34","22"};
  19. for(int i=0;i<6;i++){
  20. HashMap<String, Object> map=new HashMap<String, Object>();
  21. map.put("friend_image", R.drawable.icon);
  22. map.put("friend_username", name[i]);
  23. map.put("friend_id", id[i]);
  24. listData.add(map);
  25. }
  26. //适配器
  27. SimpleAdapter listItemAdapter= new SimpleAdapter(this,
  28. listData,
  29. R.layout.item,
  30. new String[] { "friend_image", "friend_username", "friend_id" },
  31. new int[] { R.id.friend_image, R.id.friend_username, R.id.friend_id });
  32. list.setAdapter(listItemAdapter);
  33. }
  34. }
复制代码
主要布局文件
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>
复制代码
源码下载:  BottomTest.zip (56.23 KB, 下载次数: 90)
android
0 0
原创粉丝点击