Android中自定义ListView中各项的背景色

自己总结一下

最最笨的办法就是在getView里面处理down,up,move事件,处理,繁琐,不容易正确,也无法体现android中界面与逻辑分离的优点。


最后很简单就解决了。

1. 在drawable文件夹下,新建一个listitem_bk.xml文件,其中内容为:

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    
    <item android:state_pressed="true" android:drawable="@color/solid_yellow"></item>
    <item android:state_pressed="false" android:drawable="@color/white"></item>
 
</selector>

2.设定ListView风格为:

    <ListView
        android:id="@+id/listView1"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:background="@color/white"
        android:listSelector="@color/transparent">
    </ListView>


3. 在列表适配器中,这样做。。。

public class MyAdapter extends BaseAdapter {
// ...
        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            // TODO Auto-generated method stub
            // ........
            convertView.setBackgroundResource(R.drawable.listitem_bk);
            
            return convertView;
        }



}


看,就这样就OK了。至于ListView是什么内容,自己搞定。。。害羞
















参考了2篇文章

1. http://hi.baidu.com/mendynew/item/b408a0a89a86c317a9cfb715

自定义Android的ListView布局和各Item的背景色

    原创by Mendynew,转载请注明地址

      Android中的ListView是用得非常频繁的一种组件,同时ListView也是一种很强大的组件,你可以为每一行自定义布局,也可以修改各行的背景色。自定义布局比较容易,自己实现一个layout的布局文件,然后在adapter的getView里读入就可以了。需要注意的是,在getView中不需要每次都加载layout文件,因为ListView会重复利用已生成的Item。所以每次拖动上下滚动条的时候其实每行的Item变化的只是显示的内容,就窗体本身而言是不变的,Android SDK里自带的例子是最好的说明。

        /**
         * Make a view to hold each row.
         *
         * @see android.widget.ListAdapter#getView(int, android.view.View,
         *      android.view.ViewGroup)
         */
        public View getView(int position, View convertView, ViewGroup parent) {
            // A ViewHolder keeps references to children views to avoid unneccessary calls
            // to findViewById() on each row.
            ViewHolder holder;

            // When convertView is not null, we can reuse it directly, there is no need
            // to reinflate it. We only inflate a new View when the convertView supplied
            // by ListView is null.
            if (convertView == null) {
                convertView = mInflater.inflate(R.layout.list_item_icon_text, null);

                // Creates a ViewHolder and store references to the two children views
                // we want to bind data to.
                holder = new ViewHolder();
                holder.text = (TextView) convertView.findViewById(R.id.text);
                holder.icon = (ImageView) convertView.findViewById(R.id.icon);

                convertView.setTag(holder);
            } else {
                // Get the ViewHolder back to get fast access to the TextView
                // and the ImageView.
                holder = (ViewHolder) convertView.getTag();
            }

            // Bind the data efficiently with the holder.
            holder.text.setText(DATA[position]);
            holder.icon.setImageBitmap((position & 1) == 1 ? mIcon1 : mIcon2);

            return convertView;
        }

        static class ViewHolder {
            TextView text;
            ImageView icon;
        }
    }

 

Android的界面开发模式决定了实现某一种风格的组件存在很多种方法,而如果没有对它的界面框架有个比较全面的理解的话往往实现起来要走很多弯路,譬如给各item设置背景色。因为在邮件列表中要显示两种颜色,已经阅读过的和未读的邮件以不同的背景色标识。

在item的layout文件里只能设置一中固定的颜色,这当然不是我想要的。

最直接的思路就是在Adapter的getView中根据position的不同来设置不同的背景色,但是设置了不同颜色后发现在屏幕上选中一行时背景色没有变化,选中跟没选中的颜色是一样的。于是又重新设置selector,但仍然不起作用。

看来getView中返回的View就是ListView中各行最终显示界面,所以又想着先在ListView的OnItemClickListener中记录当前选中的Item,然后在getView中判断是不是该行,如果是,就设置为选中的背景色。但是这种方法存在很大的问题,第一个问题就是onItemClickListener是在用户点击之后调用的,所以背景色的改变也是用户点完了之后才发生,而正确的应该是press的一瞬间改变背景色。第二个问题是,再返回到该ListView时需要在代码里重新清楚选中行的记录,否则该行的背景色不会刷新。

最终的解决方法是这样的:

实现两个selector文件(正常显示下有几种背景色就需要几个这样的selector文件)

mail_read_bg.xml
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item
 android:state_selected="false"
    android:state_pressed="false"
    android:drawable="@color/ltgray" />
<item android:state_pressed="true"
    android:drawable="@color/red" />
<item android:state_selected="true"
 android:state_pressed="false"
    android:drawable="@color/red" />
</selector>

mail_unread_bg.xml
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item
 android:state_selected="false"
    android:state_pressed="false"
    android:drawable="@color/white" />
<item android:state_pressed="true"
    android:drawable="@color/red" />
<item android:state_selected="true"
 android:state_pressed="false"
    android:drawable="@color/red" />
</selector>

在getView中根据邮件不同的状态设置不同的颜色方案
if unread
    convertView.setBackgroundResource(R.drawable.mail_unread_bg);
 else
    convertView.setBackgroundResource(R.drawable.mail_read_bg);

//

2. http://www.cnblogs.com/loulijun/archive/2012/04/15/2450312.html

默认情况下使用ListView背景色是黑色,选中item的高亮颜色是菊黄色,很多时候不得不自己定义背景色或者背景图

android:cacheColorHint="@android:color/transparent",意思为去黑色底色,比如ListView滚动时会刷新界面,默认颜色还是系统颜色,所以采用这种方式设置其为透明即可,这个属性在ListView中使用圆角图片来设置ListView时很有用

android:divider="@null"用于去掉listview的item之间的黑线

1、背景色

即在list_item_color_bg.xml中通过设置color来实现点击item时不同的颜色,但是如果使用color的话,listview无法使用android:listSelector属性,如果设置android:listSelector方式的话,点击一个item后整体的ListView全部都会变成一种颜色,这时必须采用在item中设置android:background的方式才可以。android:listSelector方式适用于图片的方式,即类似与(android:drawable="@drawable/img")

<?xml version="1.0" encoding="utf-8"?>
<selector
  xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:state_pressed="true" android:drawable="@color/green"></item>
    <item android:drawable="@color/white"></item>
</selector>

color.xml

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <color name="white">#ffffff</color>
    <color name="black">#000000</color>
    <color name="green">#00ff00</color>
</resources>

下面再看看布局文件

listview.xml,用color的方式,这里不能使用listSelector

复制代码
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    >
    <ListView 
        android:id="@+id/lv"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:fastScrollEnabled="true"
        android:cacheColorHint="@android:color/transparent"
        android:divider="@null"
        />
</LinearLayout>
复制代码

list_item_color.xml,通过color设置直接在item的布局中设置背景即可

复制代码
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="horizontal"
    android:background="@drawable/list_item_color_bg">

    <ImageView
        android:id="@+id/img"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        />
    <LinearLayout 
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical"
        >
        <TextView
            android:id="@+id/tv"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textSize="20sp"
        />
        <TextView 
            android:id="@+id/info"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textSize="14sp"
            />
    </LinearLayout>
</LinearLayout>
复制代码

效果图

2、背景图

这种方式是在selector文件中采用图片来设置item的背景,无论是设置ListView的android:listSelector的方式还是设置item的android:background的方式都可以使用,不过最好还是使用android:background的方式,因为使用android:listSelector的方式时下面的selector文件中设置的默认时的图片

<item android:drawable="@drawable/login_input"/>)不会显示,而改为background的方式则可以。有些奇怪,希望懂的能指点一下

<?xml version="1.0" encoding="utf-8"?>
<selector
  xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:state_pressed="true" android:drawable="@drawable/input_over"/>
    <item android:drawable="@drawable/login_input"/>
</selector>

listView此时设置如下,这里在item中不设置android:background

复制代码
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    >
    <ListView 
        android:id="@+id/lv"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:fastScrollEnabled="true"
        android:cacheColorHint="@android:color/transparent"
        android:listSelector="@drawable/list_item_drawable_bg"
        />
</LinearLayout>
复制代码

此时的效果图如下:背景图是.9.png图片,注意默认的白色.9.png图片login_input没有显示

如果使用android:background的方式,取消android:listSelector的方式,效果如下


  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值