In Android, shape is often used to customize the background of some views.
You can modify attributes such as the background color and shape of a view.
Generally, shape is written to an XML file. Today, we have a requirement that the view shape is rounded, but the color is set in the code.
The idea was to first set the color of the view in the code, and then set the solid attribute to transparent color in the shape.
<? XML version = "1.0" encoding = "UTF-8"?> <Shape xmlns: Android = "http://schemas.android.com/apk/res/android"> <corners Android: topleftradius = "8dp" Android: toprightradius = "8dp" Android: bottomleftradius = "8dp" Android: bottomrightradius = "8dp"/> <solid Android: color = "#00000000"/> </shape>
View. setbackgroundcolor (color); view. setbackgrounddrawable (R. drawable. Shape );
Unfortunately, the background cannot be replaced every time I set the background.
Finally, the Internet Google, find the solution of the http://stackoverflow.com/questions/16775891/how-to-change-solid-color-from-the-code
Gradientdrawable mygrad = (gradientdrawable) view. getbackground (); mygrad. setcolor (color );