开发者

Changing Layout Background Colors in Java

I recently made an app with several XML layouts (and I gave them all very colorful backgrounds too!) However, my friend noted it was a little 'too' colorful.

So, I decided to add a checkbox to the main.xml file, which is checked by default. If unchecked, I want every background color to go black, my buttons' colors to change their background开发者_Python百科 color from "@drawable/buttoncolor" to "@drawable/colorless", and all the text in the buttons to change to white ("#FFFFFF"). Then if checked again, the program should restore to default.

So the question is... how would I do this? I already established something like this:

View colorBox = findViewById(R.id.noColor);
colorBox.setOnClickListener(this);

where colorBox is my checkbox.

And later on...

public void onClick(View v) {
    switch (v.getId()) {
    // (Other code I have here)
    case R.id.noColor:
        // Does something go here..?
        break;
    }
}

All help is appreciated.

(On a side note, this solution didn't work for me either: How to change a TextView's background color with a color defined in my values/colors.xml file?)


colorBox.setBackgroundResource(R.drawable.colorless);
((TextView)colorBox).setTextColor(COLOR.WHITE);

you need to import java.awt.Color


Look for setBackgroundResource or setBackgroundDrawable and then call either of these methods on each view to change there background.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜