简体   繁体   English

在Android中更改自定义Listview项目背景颜色

[英]Change Custom Listview Item Background Color in Android

I want to change Custom Listview 's each Item Background Color. 我想更改Custom Listview的每个项目背景颜色。 So with Adapter, I use getView() but It is not working. 因此,使用适配器时,我使用了getView()但是它不起作用。 How to do this ? 这个怎么做 ?

My Code is as below : 我的代码如下:

adapter = new SimpleAdapter(this, aaReportData, R.layout.report_card1, new String[] { "Topic" }, new int[] { R.id.tvTopic}) {

                @Override
                public View getView(int position, View convertView,
                        ViewGroup parent) {
                    // TODO Auto-generated method stub
                    convertView.setBackgroundColor(R.color.lightish);
                    return convertView;
                }
            };

As per @Dhaval said, I defined background color to parent layout and It worked fine. 正如@Dhaval所说,我为父级布局定义了背景色,并且效果很好。 Now, I want some space between each List Item so I used Drawable and set color and border bottom as white for some space. 现在,我希望每个列表项之间都有一些空间,因此我使用了Drawable并将颜色和边框底部设置为白色以保留一些空间。

It is working fine now and I got result as per my requirement. 现在工作正常,并且按我的要求得到了结果。 Thank you so much all of you. 非常感谢大家。

you should use setBackgroundResource() method instead of setBackgroundColor() 您应该使用setBackgroundResource()方法而不是setBackgroundColor()

tryout following code 试用以下代码

view.setBackgroundResource(R.color.blue)
adapter = new SimpleAdapter(this, aaReportData, R.layout.report_card1, new String[] { "Topic" }, new int[] { R.id.tvTopic}) {

            @Override
            public View getView(int position, View convertView,
                    ViewGroup parent) {
                View v = super.getView(position, convertView, parent);
                // TODO Auto-generated method stub
                v.setBackgroundResource(R.color.lightish);
                return convertView;
            }
        };
convertView = View.inflate(mContext, R.layout.list_item, null);
convertView.setBackgroundColor(mContext.getResources().getColor(R.color.lightish));

You should inflate list item view use above line to set color to view regards. 您应该在上方的行中充气列表项视图,以设置颜色以使用。

Use this selector xml in you ListView : ListView使用此选择器xml

<item>
<shape android:shape="rectangle">
   <gradient
       android:startColor="@android:color/holo_orange_light"
       android:endColor="@android:color/holo_red_light"
       android:angle="270"/>
   </shape>
</item>

Updated: 更新:

Make changes in your ListView like: 在您的ListView更改,例如:

    <ListView
    android:id="@+id/listView_contact"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:divider="@android:color/darker_gray"
    android:dividerHeight="0.3dp"
    android:background="@android:color/transparent"
    android:listSelector="@drawable/productlistselector">
</ListView>

You can use divider and set its background transparent instead of designing new custom Background.. 您可以使用divider并将其背景设置为transparent而不用设计新的自定义背景。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM