Android布局背景颜色更改

时间:2011-09-12 15:29:21

标签: android background

我在谷歌搜索了很多,而且在这个网站上也广泛搜索,如果我错过了回答我的问题,我很抱歉。但在这里:

            public void onClick(View v){
            Button btt= (Button) findViewById(R.id.bttROnOff);
            LinearLayout ll = (LinearLayout) findViewById(R.id.layScreen);
            if ((btt.getText()).toString().compareToIgnoreCase("Reading Mode OFF")==0) {
                ll.setBackgroundColor(R.color.paleYellow);
                WindowManager.LayoutParams lp = getWindow().getAttributes();
                lp.screenBrightness = originalBrightness;
                getWindow().setAttributes(lp); 
                btt.setText("Reading Mode ON");
            }
            else {
                ll.setBackgroundColor(Color.WHITE);
                WindowManager.LayoutParams lp = getWindow().getAttributes();
                lp.screenBrightness = 1;
                getWindow().setAttributes(lp); 
                btt.setText("Reading Mode OFF");
            }
            }

我有一个按钮可以将背景颜色更改为白色,然后更改为在strings.xml文件中定义的“paleYellow”。在我的xml布局文件中,它以这种颜色开始,当我按下按钮时,它变为白色。但是,如果我按下按钮返回上一个按钮,我得到的是黑色背景。如果我改用:

ll.setBackgroundColor(Color.Yellow);

它有效,但是:

ll.setBackgroundColor(R.color.paleYellow);

不:S

3 个答案:

答案 0 :(得分:2)

setBackgroundColor使用代表a color in sRGBint,而R.color.paleYellow是颜色的ID,但不是相同的代表。要使用它,您应该致电setBackgroundResource(R.color.paleYellow)

答案 1 :(得分:1)

你有没有试过像......

Resources myRes = getResources();
int colorPaleYellow = myRes.getColor(R.color.paleYellow);
setBackgroundColor(colorPaleYellow);

或者类似......

setBackgroundcolor(this.getColor(R.color.paleYellow));

答案 2 :(得分:0)

这应该这样做:

ll.setBackgroundColor(getResources().getColor(R.color.paleYellow));