搞定了listview item带背景selector的程序共享一下.docx
《搞定了listview item带背景selector的程序共享一下.docx》由会员分享,可在线阅读,更多相关《搞定了listview item带背景selector的程序共享一下.docx(7页珍藏版)》请在冰豆网上搜索。
搞定了listviewitem带背景selector的程序共享一下
搞定了listviewitem带背景selector的程序,共享一下
我的方案是:
选择时的背景图片,是一个带颜色的透明背景。
+android:
drawSelectOnTop="true"忙乎了半天,谢谢版主的帮助,终于搞定了,是在iGoogle提供的例子基础上改的
先上图main.xml:
<?
xmlversion="1.0"encoding="utf-8"?
>
<LinearLayoutxmlns:
android="
android:
orientation="vertical"
android:
layout_width="fill_parent"
android:
layout_height="fill_parent">
<LinearLayout
android:
id="@+id/listLinearLayout"
android:
layout_height="wrap_content"
android:
orientation="vertical"
android:
layout_width="fill_parent">
<!
--我们会自己定义listview的显示方式(在另外一个布局文件里边)不用默认的方式如果自定义listview的显示方式这里这个
android:
id="@id/android:
list"必须这样写
-->
<ListViewandroid:
id="@id/android:
list"android:
layout_width="fill_parent"
android:
layout_height="wrap_content"android:
drawSelectorOnTop="false"
android:
listSelector="#00000000"
android:
scrollbars="vertical"/>
<!
--android:
drawSelectOnTop="false"此属性用来设置listview上的背景颜色会不会挡住(覆盖)内容
如果这是为false就表示不会覆盖掉,这个大家拿例子测试一下效果就明白了
-->
</LinearLayout>
</LinearLayout>复制代码lvitem_bg.xml<?
xmlversion="1.0"encoding="utf-8"?
>
<!
--Copyright(C)2007TheAndroidOpenSourceProjectLicensedundertheApacheLicense,Version2.0(the"License");
youmaynotusethisfileexceptincompliancewiththeLicense.
YoumayobtainacopyoftheLicenseat
http:
//www.apache.org/licenses/LICENSE-2.0
Unlessrequiredbyapplicablelaworagreedtoinwriting,software
distributedundertheLicenseisdistributedonan"ASIS"BASIS,
WITHOUTWARRANTIESORCONDITIONSOFANYKIND,eitherexpressorimplied.
SeetheLicenseforthespecificlanguagegoverningpermissionsand
limitationsundertheLicense.
--><selectorxmlns:
android="
<itemandroid:
state_pressed="true"android:
drawable="@drawable/gallery_photo_1"/>
<itemandroid:
drawable="@drawable/gallery_photo_2"/>
</selector>复制代码user.xml<?
xmlversion="1.0"encoding="utf-8"?
>
<!
--此布局文件用来定义listview的显示方式-->
<LinearLayoutxmlns:
android="
android:
orientation="horizontal"
android:
layout_width="fill_parent"
android:
layout_height="fill_parent"
android:
paddingLeft="10dip"
android:
paddingRight="10dip"
android:
paddingTop="1dip"
android:
paddingBottom="1dip"
android:
background="@drawable/lvitem_bg"
>
<TextView
android:
id="@+id/user_name"
android:
layout_width="180dip"
android:
layout_height="30dip"
android:
textSize="10pt"
android:
singleLine="true"/>
<TextView
android:
id="@+id/user_ip"
android:
layout_width="fill_parent"
android:
layout_height="fill_parent"
android:
gravity="right"
android:
textSize="10pt"/>
</LinearLayout>复制代码代码:
packagexiaohang.zhimeng;importjava.util.ArrayList;
importjava.util.HashMap;
importandroid.app.ListActivity;
importandroid.os.Bundle;
importandroid.view.View;
importandroid.widget.ListView;
importandroid.widget.SimpleAdapter;publicclassActivity01extendsListActivity{
//ListActivity一个以列表的方式显示数据源、数组的Activity
//ListActivityClassOverview(此描述摘自官方文档说的非常清楚了)
/*Anactivitythatdisplaysalistofitemsbybinding
toadatasourcesuchasanarrayorCursor,andexposeseventhandlers
whentheuserselectsanitem.ListActivityhostsaListViewobjectthatcanbebound
todifferentdatasources,typicallyeitheranarrayoraCursorholding
queryresults.Binding,screenlayout,androwlayoutarediscussedin
thefollowingsections.ScreenLayoutListActivityhasadefaultlayoutthatconsistsofa
single,full-screenlistinthecenterofthescreen.However,ifyou
desire,youcancustomizethescreenlayoutbysettingyourownview
layoutwithsetContentView()inonCreate().Todothis,yourownview
MUSTcontainaListViewobjectwiththeid"@android:
id/list"(orlist
ifit'sincode)Optionally,yourcustomviewcancontainanotherview
objectofanytypetodisplaywhenthelistviewisempty.This"empty
list"notifiermusthaveanid"android:
empty".Notethatwhenanempty
viewispresent,thelistviewwillbehiddenwhenthereisnodatato
display.Thefollowingcodedemonstratesan(ugly)customscreen
layout.Ithasalistwithagreenbackground,andanalternatered"no
data"message.*/
@Override
publicvoidonCreate(BundlesavedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
ArrayList<HashMap<String,String>>list=newArrayList<HashMap<String,String>>();
HashMap<String,String>map1=newHashMap<String,String>();
HashMap<String,String>map2=newHashMap<String,String>();
HashMap<String,String>map3=newHashMap<String,String>();
//一个map对象对应一条数据
map1.put("user_name","zhangsan");
map1.put("user_ip","192.168.0.1");
map2.put("user_name","lisi");
map2.put("user_ip","192.168.0.2");
map3.put("user_name","wangwu");
map3.put("user_ip","192.168.0.3");
list.add(map1);
list.add(map2);
list.add(map3);
//这里对SimpleAdapter这个构造方法的参数说明一下E文好的直接看E文
/*contextThecontextwheretheViewassociatedwiththisSimpleAdapterisrunning
dataAListofMaps.EachentryintheList
correspondstoonerowinthelist.TheMapscontainthedataforeach
row,andshouldincludealltheentriesspecifiedin"from"
resourceResourceidentifierofaviewlayout
thatdefinestheviewsforthislistitem.Thelayoutfileshould
includeatleastthosenamedviewsdefinedin"to"
fromAlistofcolumnnamesthatwillbeaddedtotheMapassociatedwitheachitem.
toTheviewsthatshoulddisplaycolumninthe
"from"parameter.TheseshouldallbeTextViews.ThefirstNviewsin
thislistaregiventhevaluesofthefirstNcolumnsinthefrom
parameter.*/
/**
*参数一Context这个不说了
*参数二就是上边声明的那个ArrayList对象
*参数三这个参数用来指定我们一行数据的key也就是一个map对象的key上下结合看一下因为我们一条数据也就是一行
*对应一个map对象一个map对象包含2个数据即user_name和user_ip这个参数就是用来指定这2个key这里是通过String数组的方式
*参数四大家一看就知道了意思是user_name这条数据用R.id.user_name这个TextView显示user_ip这条数据用
*R.id.user_ip显示
*/
SimpleAdapterlistAdapter=newSimpleAdapter(this,list,
R.layout.user,newString[]{"user_name","user_ip"},
newint[]{R.id.user_name,R.id.user_ip});
//这是AdaptersetListAdapter()此方法来自ListActivity
setListAdapter(listAdapter);
}
//当我们点击一条数据或者说一行时触发的Click事件
@Override
protectedvoidonListItemClick(ListViewl,Viewv,intposition,longid){
super.onListItemClick(l,v,position,id);
//我们输出它的ID和position
//ID
System.out.println("id------------>"+id);
//位置
System.out.println("position--------->"+position);
}
}复制代码xh_listview_Test.zip
(62.72KB,下载次数:
93)