简体   繁体   English

设置背景颜色:Android

[英]set background color: Android

How Do I set the background color of my android app.如何设置我的 android 应用程序的背景颜色。 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: 而不是#rrggbb你应该使用rr,gg和bb的十六进制值0到F:

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

Source 资源

From documentation: 来自文档:

public static int parseColor (String colorString): public static int parseColor(String colorString):

Parse the color string, and return the corresponding color-int. 解析颜色字符串,并返回相应的color-int。 If the string cannot be parsed, throws an IllegalArgumentException exception. 如果无法解析字符串,则抛出IllegalArgumentException异常。 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 #AARRGGBB'red','blue','green','black','white','grey','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 logcat 所以我相信如果你使用#rrggbb ,你的logcat中会出现IllegalArgumentException

Source 资源

Alternative: 替代方案:

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

Source 资源

This question is a old one but it can help for others too. 这个问题很古老,但也可以帮助其他人。

Try this : 试试这个 :

    li.setBackgroundColor(getResources().getColor(R.color.blue));

    or

    li.setBackgroundColor(getResources().getColor(android.R.color.red));

    or

    li.setBackgroundColor(Color.rgb(226, 11, 11));


    or
    li.setBackgroundColor(Color.RED)

Try this: 试试这个:

li.setBackgroundColor(android.R.color.red); //or which ever color do you want

EDIT: Posting logcat file would also help. 编辑:发布logcat文件也会有所帮助。

By the way, a good tip on quickly selecting color on the newer versions of AS is simply to type #fff and then using the color picker on the side of the code to choose the one you want. 顺便说一句,在较新版本的AS上快速选择颜色的一个好建议就是输入#fff,然后使用代码侧面的颜色选择器来选择你想要的颜色。 Quick and easier than remembering all the color hexadecimals. 比记住所有颜色的十六进制更快更容易。 For example: 例如:

android:background="#fff"
        int R = 111;    //My be assigned dynamic value
        int G = 111;    //My be assigned dynamic value
        int B = 111;    //My be assigned dynamic value
        
        getWindow().getDecorView().setBackgroundColor(Color.rgb(R, G, B));

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM