While using shape, use the code to modify the color attribute of shape.

Source: Internet
Author: User
Tags transparent color

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 );


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.