Android:在创建时设置随机颜色背景 [英] Android: Set Random colour background on create

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

问题描述

我想要的是当我加载我的应用程序,随机从一个预定义的字符串列表中存储一个值xml文件中称为颜色的某种彩色背景。



我现在有一个颜色集作为背景使用eclipse中的gui编辑器通过字符串颜色代码定义。



对于我的生活,无法获取背景,随机选择9个字符串之一,并在每次活动激活时显示。 p>

这方面的指导将是无价的。

解决方案

在colors.xml中

 <?xml version =1.0encoding =utf-8?> 
< resources>

< item name =bluetype =color>#FF33B5E5< / item>
< item name =purpletype =color>#FFAA66CC< / item>
< item name =greentype =color>#FF99CC00< / item>
< item name =orangetype =color>#FFFFBB33< / item>
< item name =redtype =color>#FFFF4444< / item>
< item name =darkbluetype =color>#FF0099CC< / item>
< item name =darkpurpletype =color>#FF9933CC< / item>
< item name =darkgreentype =color>#FF669900< / item>
< item name =darkorangetype =color>#FFFF8800< / item>
< item name =darkredtype =color>#FFCC0000< / item>

< integer-array name =androidcolors>
< item> @ color / blue< / item>
< item> @ color / purple< / item>
< item> @ color / green< / item>
< item> @ color / orange< / item>
< item> @ color / red< / item>
< item> @ color / darkblue< / item>
< item> @ color / darkpurple< / item>
< item> @ color / darkGreen< / item>
< item> @ color / darkorange< / item>
< item> @ color / darkred< / item>
< / integer-array>

< / resources>

在onCreate()

  int [] androidColors = getResources()。getIntArray(R.array.androidcolors); 
int randomAndroidColor = androidColors [new Random()。nextInt(androidColors.length)];
view.setBackgroundColor(randomAndroidColor);


What I want is when I load my app up it to randomly have a certain colored background from a predefined list of strings stored in a values xml file called colours.

What I currently have is one colour set as the background defined through the string colour code using the gui editor in eclipse.

For the life of me can't work out how to get the background to randomly pick one of the 9 strings and display it each time the activity is activated.

Guidance on this would be invaluable.

解决方案

In colors.xml

<?xml version="1.0" encoding="utf-8"?>
<resources>

    <item name="blue" type="color">#FF33B5E5</item>
    <item name="purple" type="color">#FFAA66CC</item>
    <item name="green" type="color">#FF99CC00</item>
    <item name="orange" type="color">#FFFFBB33</item>
    <item name="red" type="color">#FFFF4444</item>
    <item name="darkblue" type="color">#FF0099CC</item>
    <item name="darkpurple" type="color">#FF9933CC</item>
    <item name="darkgreen" type="color">#FF669900</item>
    <item name="darkorange" type="color">#FFFF8800</item>
    <item name="darkred" type="color">#FFCC0000</item>

    <integer-array name="androidcolors">
        <item>@color/blue</item>
        <item>@color/purple</item>
        <item>@color/green</item>
        <item>@color/orange</item>
        <item>@color/red</item>
        <item>@color/darkblue</item>
        <item>@color/darkpurple</item>
        <item>@color/darkgreen</item>
        <item>@color/darkorange</item>
        <item>@color/darkred</item>
    </integer-array>

</resources> 

In onCreate()

int[] androidColors = getResources().getIntArray(R.array.androidcolors);
int randomAndroidColor = androidColors[new Random().nextInt(androidColors.length)];
view.setBackgroundColor(randomAndroidColor);

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

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