简体   繁体   English

Android ListView背景颜色更改

[英]Android ListView background color change

I have ListView and I want to change currently active row to be different color. 我有ListView,我想将当前活动的行更改为不同的颜色。

Currently I have this: 目前我有这个:

rowView.setBackgroundColor( 0xFF0000FF ); // make blue bg

And it works as expected. 它按预期工作。

However there is one "philosophical" problem - since I use all default color schemes, what color I should use? 但是,存在一个“哲学”问题-由于我使用所有默认的配色方案,应该使用哪种颜色? Blue is poor example, because android scheme may be dark or light or completely custom. 蓝色是一个较差的示例,因为android方案可能是深色或浅色或完全自定义。

Things I have in mind could be inverse color, sightly similar color (say 20% darker) or probably same color, but with some effect. 我想到的东西可能是反色,看似相似的颜色(例如深20%)或可能是相同的颜色,但有一定效果。

Must work on Android 2.2+ 必须适用于Android 2.2+

Edited: 编辑:

Solution I am looking for is more something like this: 我正在寻找的解决方案是这样的:

int x = this.getResources().getColor(android.R.color.holo_blue_bright);
listViewItem.setBackgroundColor(x);

If you know this is a color then you can try 如果您知道这是一种颜色,则可以尝试

    ColorDrawable buttonColor = (ColorDrawable) button.getBackground();

And if you're on Android 3.0+ you can get out the resource id of the color. 如果您使用的是Android 3.0+,则可以获取颜色的资源ID。

    int colorId = buttonColor.getColor();

Then, do the following: 然后,执行以下操作:

    int red = Color.red(colorId);
    int blue = Color.blue(colorId);
    int green = Color.green(colorId);
    double percent = 400;
    button.setBackgroundColor(Color.rgb((int)(red * percent/100),
        (int)(blue * percent/100),(int)(green * percent/100)));

Note making the color higher brightens the color, and a lower number darkens the color. 请注意,使颜色越高,颜色越亮;而数字越小,颜色越暗。

If you want a function to calculate colors darker or brighter see Brighter/Darker function . 如果要使用函数来计算较暗或较亮的颜色,请参见“较亮/较暗”功能 Is in javascript but is easy to translate to java 在javascript中,但易于转换为java

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

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