简体   繁体   中英

Android ListView background color change

I have ListView and I want to change currently active row to be different color.

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.

Things I have in mind could be inverse color, sightly similar color (say 20% darker) or probably same color, but with some effect.

Must work on 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.

    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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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