listview Button始终放在底部示例

2016-02-19 09:00 3 1 收藏

下面图老师小编跟大家分享一个简单易学的listview Button始终放在底部示例教程,get新技能是需要行动的,喜欢的朋友赶紧收藏起来学习下吧!

【 tulaoshi.com - 编程语言 】

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 弹出输入法时显示的底部按钮
项目只是实现主要的数据填充及布局,故只是简单的文件加载。以下是源码:
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);
//存储数据的数组列表
ArrayListHashMapString, Object listData=new ArrayListHashMapString,Object();
String []name={"William","Charles","Linng","Json","Bob","Carli"};
String []id={"12","16","33","21","34","22"};
for(int i=0;i6;i++){
HashMapString, Object map=new HashMapString, 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

来源:https://www.tulaoshi.com/n/20160219/1589114.html

延伸阅读
下拉刷新在越来越多的App中使用,已经形成一种默认的用户习惯,遇到列表显示的内容时,用户已经开始习惯性的拉拉。在交互习惯上已经形成定性。之前在我的文章《IOS学习笔记34—EGOTableViewPullRefresh实现下拉刷新》中介绍过如何在IOS上实现下拉刷新的功能。今天主要介绍下在Android上实现下拉刷新的Demo,下拉控件参考自Github上开源项目Pull...
标签: Web开发
style type="text/css"    #oContainer {          width: 600px;          height: 500px;          border: 1px solid menu;   ...
SQL的意思是结构化查询语言,其主要功能是同各种数据库建立联系,进行沟通.查询指的是对存储于SQL的数据的请求。查询要完成的任务是:将 Select 语句的结果集提供给用户。Select 语句从 SQL 中检索出数据,然后以一个或多个结果集的形式将其返回给用户。  ======================================================...
标签: Web开发
CSS的简单在于它易学,CSS的困难在于寻找更好的解决方案。在CSS的世界里,似乎没有完美这种说法。所以,现在介绍的CSS绝对底部,只是目前个人见过的方案中比较完美的吧。 先说我们为什么会使用到这个CSS底部布局解决方案: 当做一个页面时,如果页面内容很少,不足于填充一屏的窗口区域,按普通的布局,就会出现下面图片中的样子(也就是底...
标签: Web开发
鼠标提示 body{font-size:12px}

经验教程

417

收藏

19
微博分享 QQ分享 QQ空间 手机页面 收藏网站 回到头部