设置背景颜色:机器人 [英] set background color: Android
问题描述
如何设置我的Android应用程序的背景色。当我尝试:
的LinearLayout李=(的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)
从技术文档:
公共静态INT parseColor(字符串colorString):
解析颜色字符串,并返回相应的颜色-INT。如果
字符串不能被解析,抛出IllegalArgumentException异常。
支持的格式有:#RRGGBB #AARRGGBB'红','蓝','绿色',
黑,白,灰色,青色,洋红,黄,浅灰色,
深灰,灰色,浅灰,darkgrey,水上,褐色,
石灰,圆子,海军,橄榄树,紫色,银,水鸭
块引用>所以,我相信,如果你使用的是
#RRGGBB
你得到的抛出:IllegalArgumentException 在logcat中<一个href=\"http://developer.android.com/reference/android/graphics/Color.html#parseColor%28java.lang.String%29\">Source
备选:
颜色mColor =新的色彩();
mColor.red(redvalue);
mColor.green(greenvalue);
mColor.blue(bluevalue);
li.setBackgroundColor(mColor);<一个href=\"http://stackoverflow.com/questions/15717977/android-i-have-error-with-exception-color-parsecolor\">Source
How Do I set the background color of my android app. When I try:
LinearLayout li=(LinearLayout)findViewById(R.id.myLayout); li.setBackgroundColor(Color.parseColor("#rrggbb"));
My app always crashes. Could someone help me out. Thanks
解决方案Color.parseColor("#rrggbb")
instead of
#rrggbb
you should be using hex values 0 to F for rr, gg and bb:e.g.
Color.parseColor("#000000")
orColor.parseColor("#FFFFFF")
From documentation:
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'
So I believe that if you are using
#rrggbb
you are getting IllegalArgumentException in your logcatAlternative:
Color mColor = new Color(); mColor.red(redvalue); mColor.green(greenvalue); mColor.blue(bluevalue); li.setBackgroundColor(mColor);
这篇关于设置背景颜色:机器人的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!