listview Button始终放在底部示例

时间:2022-06-21 04:53:01

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。
listview Button始终放在底部示例 
图-1 三层的底部布局界面
listview Button始终放在底部示例 
图 2 弹出输入法时显示的底部按钮
项目只是实现主要的数据填充及布局,故只是简单的文件加载。以下是源码:
bottomtestactivity.java

复制代码 代码如下:


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;

publicclass bottomtestactivityextends activity {

/** called when the activity is first created. */
@override
publicvoid 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" },
newint[] { r.id.friend_image, r.id.friend_username, r.id.friend_id });
list.setadapter(listitemadapter);
}
}


主要布局文件
main.xml

复制代码 代码如下:


<?xmlversion="1.0"encoding="utf-8"?>
<linearlayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<relativelayoutandroid:id="@+id/bottom"
android:layout_width="fill_parent"
android:layout_height="wrap_content" >
<linearlayoutandroid:id="@+id/top"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<edittextandroid: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"/>
<buttonandroid: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>
<linearlayoutandroid: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">
<textviewandroid: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"/>
<listviewandroid:id="@+id/friends"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginbottom="6dip"/>
</linearlayout>
<linearlayoutandroid:id="@+id/bottom"
android:background="@drawable/bg"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_alignparentbottom="true" >
<buttonandroid:id="@+id/refresh"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_margintop="2dip"
android:text="刷新用户列表"
android:layout_weight="1"/>
<buttonandroid: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内容的布局文件
item.xml

复制代码 代码如下:


<?xmlversion="1.0"encoding="utf-8"?>
<relativelayoutxmlns:android="http://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">
<imageviewandroid: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"/>
<textviewandroid: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" />
<textviewandroid: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>