打开APP
userphoto
未登录

开通VIP,畅享免费电子书等14项超值服

开通VIP
搞定了listview item带背景selector的程序,共享一下
我的方案是: 选择时的背景图片,是一个带颜色的透明背景。+ android:drawSelectOnTop="true"



忙乎了半天,谢谢版主的帮助,终于搞定了,是在iGoogle提供的例子基础上改的
先上图


main.xml:
  1. <?xml version="1.0" encoding="utf-8"?>
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3.     android:orientation="vertical"
  4.     android:layout_width="fill_parent"
  5.     android:layout_height="fill_parent">
  6.   <LinearLayout
  7.                   android:id="@+id/listLinearLayout"
  8.                   android:layout_height="wrap_content"
  9.                   android:orientation="vertical"
  10.                   android:layout_width="fill_parent">
  11.                   <!-- 我们会自己定义listview的显示方式(在另外一个布局文件里边)不用默认的方式 如果自定义listview的显示方式这里这个
  12.                   android:id="@id/android:list" 必须这样写
  13.                    -->
  14.                   <ListView android:id="@id/android:list" android:layout_width="fill_parent"
  15.                           android:layout_height="wrap_content" android:drawSelectorOnTop="false"
  16.                           android:listSelector="#00000000"
  17.                           android:scrollbars="vertical"/>
  18.                           <!-- android:drawSelectOnTop="false"此属性用来设置listview上的背景颜色会不会挡住(覆盖)内容
  19.                                   如果这是为false就表示不会覆盖掉 ,这个大家拿例子测试一下效果就明白了
  20.                            -->
  21.   </LinearLayout>
  22. </LinearLayout>
复制代码
lvitem_bg.xml
  1. <?xml version="1.0" encoding="utf-8"?>
  2. <!-- Copyright (C) 2007 The Android Open Source Project

  3.      Licensed under the Apache License, Version 2.0 (the "License");
  4.      you may not use this file except in compliance with the License.
  5.      You may obtain a copy of the License at
  6.   
  7.           http://www.apache.org/licenses/LICENSE-2.0
  8.   
  9.      Unless required by applicable law or agreed to in writing, software
  10.      distributed under the License is distributed on an "AS IS" BASIS,
  11.      WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12.      See the License for the specific language governing permissions and
  13.      limitations under the License.
  14. -->

  15. <selector xmlns:android="http://schemas.android.com/apk/res/android">
  16.     <item android:state_pressed="true" android:drawable="@drawable/gallery_photo_1" />
  17.     <item android:drawable="@drawable/gallery_photo_2" />
  18. </selector>
复制代码
user.xml
  1. <?xml version="1.0" encoding="utf-8"?>
  2. <!-- 此布局文件用来定义listview 的显示方式 -->
  3. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  4.     android:orientation="horizontal"
  5.     android:layout_width="fill_parent"
  6.     android:layout_height="fill_parent"
  7.     android:paddingLeft="10dip"
  8.     android:paddingRight="10dip"
  9.     android:paddingTop="1dip"
  10.     android:paddingBottom="1dip"
  11.     android:background="@drawable/lvitem_bg"
  12.     >
  13. <TextView  
  14.         android:id="@+id/user_name"
  15.     android:layout_width="180dip"
  16.     android:layout_height="30dip"
  17.     android:textSize="10pt"
  18.     android:singleLine="true"/>
  19. <TextView
  20.         android:id="@+id/user_ip"
  21.         android:layout_width="fill_parent"
  22.         android:layout_height="fill_parent"       
  23.         android:gravity="right"
  24.         android:textSize="10pt"/>
  25. </LinearLayout>
复制代码
代码:
  1. package xiaohang.zhimeng;

  2. import java.util.ArrayList;
  3. import java.util.HashMap;
  4. import android.app.ListActivity;
  5. import android.os.Bundle;
  6. import android.view.View;
  7. import android.widget.ListView;
  8. import android.widget.SimpleAdapter;

  9. public class Activity01 extends ListActivity {
  10.    
  11.         //ListActivity一个以列表的方式显示数据源、数组的Activity
  12.         //ListActivity Class Overview(此描述摘自官方文档说的非常清楚了)
  13.         /*An activity that displays a list of items by binding to a data source such as an array or Cursor, and exposes event handlers when the user selects an item.

  14.         ListActivity hosts a ListView object that can be bound to different data sources, typically either an array or a Cursor holding query results. Binding, screen layout, and row layout are discussed in the following sections.

  15.         Screen Layout

  16.         ListActivity has a default layout that consists of a single, full-screen list in the center of the screen. However, if you desire, you can customize the screen layout by setting your own view layout with setContentView() in onCreate(). To do this, your own view MUST contain a ListView object with the id "@android:id/list" (or list if it's in code)

  17.         Optionally, your custom view can contain another view object of any type to display when the list view is empty. This "empty list" notifier must have an id "android:empty". Note that when an empty view is present, the list view will be hidden when there is no data to display.

  18.         The following code demonstrates an (ugly) custom screen layout. It has a list with a green background, and an alternate red "no data" message.*/
  19.     @Override
  20.     public void onCreate(Bundle savedInstanceState) {
  21.         super.onCreate(savedInstanceState);
  22.         setContentView(R.layout.main);
  23.         ArrayList<HashMap<String, String>> list = new ArrayList<HashMap<String,String>>();
  24.         HashMap<String, String> map1 = new HashMap<String, String>();
  25.         HashMap<String, String> map2 = new HashMap<String, String>();
  26.         HashMap<String, String> map3 = new HashMap<String, String>();
  27.         //一个map对象对应一条数据
  28.         map1.put("user_name", "zhangsan");
  29.         map1.put("user_ip", "192.168.0.1");
  30.         
  31.         map2.put("user_name", "lisi");
  32.         map2.put("user_ip", "192.168.0.2");
  33.         
  34.         map3.put("user_name", "wangwu");
  35.         map3.put("user_ip", "192.168.0.3");
  36.         list.add(map1);
  37.         list.add(map2);
  38.         list.add(map3);
  39.         //这里对SimpleAdapter这个构造方法的参数说明一下 E文好的直接看E文
  40.         /*context        The context where the View associated with this SimpleAdapter is running
  41.         data        A List of Maps. Each entry in the List corresponds to one row in the list. The Maps contain the data for each row, and should include all the entries specified in "from"
  42.         resource        Resource identifier of a view layout that defines the views for this list item. The layout file should include at least those named views defined in "to"
  43.         from        A list of column names that will be added to the Map associated with each item.
  44.         to        The views that should display column in the "from" parameter. These should all be TextViews. The first N views in this list are given the values of the first N columns in the from parameter.*/
  45.         /**
  46.          * 参数一 Context 这个不说了
  47.          * 参数二 就是上边声明的那个ArrayList对象
  48.          * 参数三 这个参数用来指定 我们一行数据 的key 也就是一个map对象的key 上下结合看一下 因为我们一条数据也就是一行
  49.          * 对应一个map对象 一个map对象包含2个数据 即 user_name 和 user_ip 这个参数就是用来指定这2个key 这里是通过String数组的方式
  50.          * 参数四  大家一看就知道了 意思是 user_name 这条数据用 R.id.user_name 这个TextView显示  user_ip 这条数据用
  51.          * R.id.user_ip 显示
  52.          */
  53.         SimpleAdapter listAdapter = new SimpleAdapter(this,list,
  54.                         R.layout.user, new String[] {"user_name","user_ip"},
  55.                         new int[] {R.id.user_name,R.id.user_ip});
  56.         //这是Adapter setListAdapter()此方法来自ListActivity
  57.         setListAdapter(listAdapter);
  58.     }
  59.     //当我们点击一条数据 或者说一行时 触发的Click事件
  60.     @Override
  61.     protected void onListItemClick(ListView l, View v, int position, long id) {
  62.             super.onListItemClick(l, v, position, id);
  63.             //我们输出它的 ID 和 position
  64.             //ID
  65.             System.out.println("id------------>" + id);
  66.             //位置
  67.             System.out.println("position--------->" + position);
  68.     }
  69. }
复制代码
xh_listview_Test.zip(62.72 KB, 下载次数: 93)


本站仅提供存储服务,所有内容均由用户发布,如发现有害或侵权内容,请点击举报
打开APP,阅读全文并永久保存 查看更多类似文章
猜你喜欢
类似文章
android layout selector使用问题
xml中,button改变背景颜色方法
ExpandableListView讲解
ListView之一:Adapter介紹與使用
Android 解决ListView 和 ScrollView 共存冲突的问题
Android提高第十三篇之探秘蓝牙隐藏API
更多类似文章 >>
生活服务
热点新闻
分享 收藏 导长图 关注 下载文章
绑定账号成功
后续可登录账号畅享VIP特权!
如果VIP功能使用有故障,
可点击这里联系客服!

联系客服