简体   繁体   中英

Does changing background color in android destroy the widget's appearance?

I've noticed that changing the background color of an android widget ( ex. Button or TextView ) by program:

myButton.setBackgroundColor(Color.BLUE);

makes it to loose its 3D shape, border, and shadows effects, and then appears like a ugly flat square. What am I missing?

Sorry for the very naive question but I couldn't get it right although tried for a time.

Rounded corners, shadow effects, etc are often accomplished in Android by using images. See this developer documentation for an explanation of how that works.

A widget can have either an image background, or a solid color background. So, by setting the background color you are override the background image. If you want to change the color without losing everything else, you need to edit the image files.

由于背景是可绘制的,您可以修改drawable以更改颜色:

myButton.getBackground().setColorFilter(Color.BLUE, PorterDuff.Mode.SRC_ATOP);

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