设置背景颜色:Android [英] set background color: Android

查看:32
本文介绍了设置背景颜色:Android的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何设置我的 android 应用程序的背景颜色.当我尝试时:

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")

而不是 #rrggbb 你应该使用十六进制值 0 到 F 来表示 rr、gg 和 bb:

instead of #rrggbb you should be using hex values 0 to F for rr, gg and bb:

例如Color.parseColor("#000000")Color.parseColor("#FFFFFF")

来源

来自文档:

public static int parseColor(String colorString):

public static int parseColor (String colorString):

解析颜色字符串,并返回对应的颜色整数.如果无法解析字符串,抛出 IllegalArgumentException 异常.支持的格式有:#RRGGBB #AARRGGBB 'red'、'blue'、'green'、黑色"、白色"、灰色"、青色"、洋红色"、黄色"、浅灰色"、深灰色"、灰色"、浅灰色"、深灰色"、浅绿色"、紫红色"、'石灰','栗色','海军','橄榄','紫色','银色','蓝绿色'

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

So I believe that if you are using #rrggbb you are getting IllegalArgumentException in your logcat

来源

替代方案:

Color mColor = new Color();
mColor.red(redvalue);
mColor.green(greenvalue);
mColor.blue(bluevalue);
li.setBackgroundColor(mColor);

来源

这篇关于设置背景颜色:Android的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆