简体   繁体   English

listview项目背景颜色更改

[英]listview item background color change

I am working on an android application. 我正在开发一个android应用程序。 I have created a listview by using 我已经使用创建了一个列表视图

setListAdapter(new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1,arrayname));   
getListView().setTextFilterEnabled(true);   

Now i want to change the selected item's color. 现在,我想更改所选项目的颜色。 I could change the background of the selected item by placing 我可以通过放置更改所选项目的背景

listviewobject.getChildAt(position).setBackgroundColor(Color.BLACK);

in onListItemClick() 在onListItemClick()中

this code is changing the background color but if I select any other list item then also the previously clicked list item's color is red.So I change the previously clicked listitem's color by 这段代码正在更改背景颜色,但是如果我选择任何其他列表项,那么先前单击的列表项的颜色也是红色。因此,我可以通过以下方式更改先前单击的列表项的颜色:

l.getChildAt(prevpos).setBackgroundColor(Color.BLACK);

now the problem is if i change the background of previously clicked listitems color to black.Then i can't see the text on that particular listitem.I i click again then only i can see the text on that item.So its look weired.please help me friends 现在的问题是,如果我将先前单击的listitems的背景颜色更改为黑色。然后我看不到该特定listitem上的文本。我再次单击则只能看到该项目上的文本,因此看起来很奇怪。请帮我朋友

After going through lots of posts and blogs i found this solution it works for me... 在浏览了许多帖子和博客之后,我发现此解决方案对我有用。

declare row variable global 全局声明行变量

public View row;

your_list.setOnItemClickListener(new OnItemClickListener() {

    public void onItemClick(AdapterView<?> a, View v,
                        int position, long id) {

    if (row != null) {
        row.setBackgroundResource(R.color.orange);
    }
    row = v;
    v.setBackgroundResource(R.color.transparent_green);
)};

Well using this in the properties of your list view mught help: 很好地在列表视图的属性中使用此帮助:

android:listSelector="@drawable/list_selector"

It will automatically set the selected drawable as background of the selected item. 它将自动将所选的可绘制对象设置为所选项目的背景。 Hope this works for you and drawable may be your own choice's color. 希望这对您有用,drawable可能是您自己选择的颜色。

Explanation: Add an image strip to your drawables folder/s and set that image in listSelector attribute of the your list view. 说明:将图像带添加到您的drawables文件夹,然后在列表视图的listSelector属性中设置该图像。 Now you will navigate through your list view, the list view's background will be of the color of the image strip you set instead of android's native color. 现在,您将在列表视图中导航,列表视图的背景将是您设置的图像带的颜色,而不是android的本机颜色。 Hope you get it now...:-) 希望你现在得到它... :-)

I this This happening because you have put text color as black and your setting the background color also black that's why you can't see the difference. 我发生这种情况是因为您将文本颜色设置为黑色,而将背景颜色设置为黑色,这就是为什么看不到差异的原因。 for setting the background color you can use the following line. 要设置背景色,您可以使用以下行。

view.setBackgroundColor(Color.YELLOW); view.setBackgroundColor(Color.YELLOW);

use different color then text color. 使用与文本颜色不同的颜色。

By default the background is black. 默认情况下,背景为黑色。 If you have customized your listview then when you scroll it would turn black. 如果您已自定义列表视图,则在滚动时它将变为黑色。 So when you define your list view set background cache color to the color you need as below: 因此,当您定义列表视图时,将背景缓存颜色设置为所需的颜色,如下所示:

    yourlistView.setCacheColorHint(Color.WHITE);

What you can do is instead of using .setBackgroundColor() with a Color value, create a Color State List and assign it with .setBackgroundResource() . 您可以做的是,不要将.setBackgroundColor()与Color值一起使用, .setBackgroundColor()创建一个颜色状态列表 ,并使用.setBackgroundResource()对其进行分配。 That way, you can define the various states that your list item can become, depending on the item's current state. 这样,您可以根据列表的当前状态定义列表项可以变为的各种状态。

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

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