简体   繁体   中英

Android. Change the background color of a FrameLayout from code

I'm trying to change the background color of a FramyLayout. The color is changing, but to the wrong one.

However it is working fine if I do it through the XML.

Here is my res/values/colors.xml code

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <color name="grey">#888888</color>
    <color name="white">#FFFFFF</color>
    <color name="red">#ffff3448</color>
    <color name="green">#ff408c3a</color>
</resources>

Here is how it looks like if I make changes in the XML 颜色改变正确

And that's what is happening if I try to do it with the code

 FrameLayout frameLayoutBalance = (FrameLayout)view.findViewById(R.id.frameLayoutBalance);
 frameLayoutBalance.setBackgroundColor(R.color.green);

颜色改变不正确

You should not use frameLayoutBalance.setBackgroundColor(R.color.green);

setBackgroundColor required a Color (ie its value as describe by Chirag Raval) not a color resources.

use this frameLayoutBalance.setBackgroundColor(getResources().getColor(R.color.green));

使用此代码frameLayoutBalance.setBackgroundColor(Color.parseColor(“#00aacc”));

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