listview Button始終放在底部示例
更新時(shí)間:2013年07月02日 15:45:42 作者:
android實(shí)現(xiàn)底部布局往往使用RelativeLayout的布局方式,以下的例子就是實(shí)現(xiàn)三層布局的底部布局的功能,感興趣的朋友可以參考下哈,希望對大家有所幫助
android實(shí)現(xiàn)底部布局往往使用RelativeLayout的布局方式,并且設(shè)置android:layout_alignParentBottom=”true”,這樣很容易實(shí)現(xiàn)底部布局。然而對于比較復(fù)雜的布局簡單的屬性設(shè)置無法達(dá)到這樣的效果,例如top,center,bottom三層的布局,很可能因?yàn)橹虚g層(center)的數(shù)據(jù)太多而將無法顯示全或者將bottom層擠下去。解決這個(gè)問題,在采用RelativeLayout布局時(shí),除了設(shè)置android:layout_alignParentBottom=”true”外,還需要對中間層進(jìn)行屬性進(jìn)行設(shè)置:android:layout_above=”@id/bottom”
android:layout_below=”@id/top”。這樣的設(shè)置即確保center層能處于中間位置,也可以通過自適應(yīng)顯示滾動條。
以下的例子就是實(shí)現(xiàn)三層布局的底部布局的功能。如圖1,2。
圖-1 三層的底部布局界面
圖 2 彈出輸入法時(shí)顯示的底部按鈕
項(xiàng)目只是實(shí)現(xiàn)主要的數(shù)據(jù)填充及布局,故只是簡單的文件加載。以下是源碼:
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);
//存儲數(shù)據(jù)的數(shù)組列表
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內(nèi)容的布局文件
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>
android:layout_below=”@id/top”。這樣的設(shè)置即確保center層能處于中間位置,也可以通過自適應(yīng)顯示滾動條。
以下的例子就是實(shí)現(xiàn)三層布局的底部布局的功能。如圖1,2。
圖-1 三層的底部布局界面
圖 2 彈出輸入法時(shí)顯示的底部按鈕
項(xiàng)目只是實(shí)現(xiàn)主要的數(shù)據(jù)填充及布局,故只是簡單的文件加載。以下是源碼:
BottomTestActivity.java
復(fù)制代碼 代碼如下:
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);
//存儲數(shù)據(jù)的數(shù)組列表
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
復(fù)制代碼 代碼如下:
<?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內(nèi)容的布局文件
item.xml
復(fù)制代碼 代碼如下:
<?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>
相關(guān)文章
Android onLoadFinished與onLoaderReset回調(diào)詳解及實(shí)例
這篇文章主要介紹了Android onLoadFinished與onLoaderReset回調(diào)詳解及實(shí)例的相關(guān)資料,需要的朋友可以參考下2017-03-03
Android自定義DataGridView數(shù)據(jù)表格控件
這篇文章主要介紹了Android自定義DataGridView數(shù)據(jù)表格控件的相關(guān)資料,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2016-11-11
Android編程實(shí)現(xiàn)播放MP3功能示例
這篇文章主要介紹了Android編程實(shí)現(xiàn)播放MP3功能,結(jié)合實(shí)例形式分析了Android播放MP3功能的界面布局與功能實(shí)現(xiàn)相關(guān)操作技巧,需要的朋友可以參考下2017-02-02
Android studio配置lambda表達(dá)式教程
Java 8的一個(gè)大亮點(diǎn)是引入Lambda表達(dá)式,使用它設(shè)計(jì)的代碼會更加簡潔。接下來通過本文給大家介紹Android studio配置lambda表達(dá)式教程,需要的朋友參考下吧2017-05-05
Android 中 MD5 的幾種生成方式(小結(jié))
這篇文章主要介紹了Android 中 MD5 的幾種生成方式,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-03-03
橫豎屏切換導(dǎo)致頁面頻繁重啟screenLayout解析
這篇文章主要為大家介紹了橫豎屏切換導(dǎo)致頁面頻繁重啟screenLayout解析,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-03-03
Android LuBan與Compressor圖片壓縮方式
本篇文章主要介紹了Android LuBan與Compressor圖片壓縮方式,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2018-04-04
Android開發(fā)之拼音轉(zhuǎn)換工具類PinyinUtils示例
這篇文章主要介紹了Android開發(fā)之拼音轉(zhuǎn)換工具類PinyinUtils,涉及Android基于pinyin4j-2.5.0.jar包文件實(shí)現(xiàn)漢字轉(zhuǎn)拼音功能的相關(guān)操作技巧,需要的朋友可以參考下2017-11-11
基于Android實(shí)現(xiàn)個(gè)性彩色好看的二維碼
二維碼在我們?nèi)粘I钪袩o處不在,今天小編通過本教程給大家介紹基于Android實(shí)現(xiàn)個(gè)性彩色好看的二維碼,需要的朋友參考下吧2016-02-02

