如何设置我的 android 应用程序的背景颜色。当我尝试时:
LinearLayout li=(LinearLayout)findViewById(R.id.myLayout);
li.setBackgroundColor(Color.parseColor("#rrggbb"));
我的应用程序总是崩溃。有人可以帮帮我。谢谢
最佳答案
Color.parseColor("#rrggbb")
而不是 #rrggbb
你应该使用十六进制值 0 到 F 来表示 rr、gg 和 bb:
例如Color.parseColor("#000000")
或 Color.parseColor("#FFFFFF")
来自文档:
public static int parseColor (String colorString):
Parse the color string, and return the corresponding color-int. If the string cannot be parsed, throws an IllegalArgumentException exception. Supported formats are: #RRGGBB #AARRGGBB 'red', 'blue', 'green', 'black', 'white', 'gray', 'cyan', 'magenta', 'yellow', 'lightgray', 'darkgray', 'grey', 'lightgrey', 'darkgrey', 'aqua', 'fuschia', 'lime', 'maroon', 'navy', 'olive', 'purple', 'silver', 'teal'
所以我相信如果你使用 #rrggbb
你会在你的 logcat 中得到 IllegalArgumentException
替代方案:
Color mColor = new Color();
mColor.red(redvalue);
mColor.green(greenvalue);
mColor.blue(bluevalue);
li.setBackgroundColor(mColor);
关于java - 设置背景颜色 : Android,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18033260/