Android布局背景颜色更改
我在谷歌上进行了相当多的搜索,也在这个网站上进行了广泛的搜索,如果我错过了回答我的问题,我很抱歉。但事情是这样的:
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
I've search quite a bit in google and extensively in this site also, if I missed the question that answer mine I'm sorry. But here it goes:
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");
}
}
I have a button to change the background color to white and then to "paleYellow" which is defined in the strings.xml file. In my xml layout file, it starts with this color and when I press the button it changes to white. But if I press the button to return to the previous one, what I get is a black background. If I use instead:
ll.setBackgroundColor(Color.Yellow);
It works, but:
ll.setBackgroundColor(R.color.paleYellow);
Does not :S
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
setBackgroundColor
采用一个int
来表示 sRGB 中的颜色,而R.color.paleYellow
是颜色的 id,但不是相同的表示形式。要使用它,您应该调用setBackgroundResource(R .color.paleYellow)
setBackgroundColor
takes anint
that represent a color in sRGB, whileR.color.paleYellow
is an id of color, but not in the same representation. to use it, you should callsetBackgroundResource(R.color.paleYellow)
你有没有尝试过类似...
或者类似...的东西
Have you tried something like...
or maybe something like...
这应该可以做到:
this should do it: