I have a horizontal line created with a view like this:

<View
            android:layout_width="fill_parent"
            android:id="@+id/led_connection"
            android:layout_height="5dip"
            android:background="#fff"
            android:layout_marginBottom="15dp" />

I want to know how can I change the color programatically. Because I am trying to use setBackground and setBackgroundDrawable but SDK sais me that it cannot applied to a View.

I'm getting the view with this:

View led_connection = (View)v.findViewById(R.id.led_connection);
有帮助吗?

解决方案

Try:

led_connection.setBackgroundColor(0xFF00FF00);

See docs for details

其他提示

If you have html colors, can try this to solve your problem.

led_connection.setBackgroundColor(Color.parseColor("#679456"));
led_connection.setBackgroundColor(Color.parseColor("html_code_colors"));

As Marcin Orlowski sais, the solution is use setBackgroundColor with an hex color with the 2 first digits that are alpha level.

I'm using for GREEN:

led_connection.setBackgroundColor(0xFF008000);  // GREEN

and for RED:

led_connection.setBackgroundColor(0xFFFF0000);  // RED

Thank you

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top