2014-09-18 140 views
5

我想要的是,當我加載我的應用程序時,它隨機從預定義的字符串列表中存儲一個預定義的字符串列表,存儲在名爲colors的值xml文件中。Android:設置創建隨機顏色背景

我現在所擁有的是一種顏色設置爲背景,使用eclipse中的gui編輯器通過字符串顏色代碼定義。

因爲我的生活不能解決如何獲得背景,隨機選擇9個字符串中的一個,並在每次激活活動時顯示它。

對此的指導將是非常寶貴的。

回答

34

在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> 

在的onCreate()

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

您可以將一個變量Random rnd = new Random();設置爲一個隨機數(將生成0到1之間的僞隨機數)。然後,你可以說:

if (rnd < 0.09) { 
     //pick first colour 
    } else if (rnd >= 0.09 && rnd < 0.18) { 
     //pick second colour 
    } else if (rnd >= 0.18 && rnd < 0.27) { 
     //pick second colour  
    } else // etc etc up to 1.0 when you will have 9 options, each with an equal chance of randomly being picked 

這種方式,隨機數rnd決定的背景是顏色每次onCreate()被調用。

4

我想我可以找到一個簡單的方法,但一些實現多久,你從顏色的定義數組中選擇隨機顏色,並將該字符串顏色解析爲背景。

<?xml version="1.0" encoding="utf-8"?> 
<resources> 
    <color name="bright_pink">#FF007F</color> 
    <color name="red">#FF0000</color> 
    <color name="orange">#FF7F00</color> 
    <color name="yellow">#FFFF00</color> 
    <color name="chartreuse">#7FFF00</color> 
    <color name="green">#00FF00</color> 
    <color name="spring_green">#00FF7F</color> 
    <color name="cyan">#00FFFF</color> 
    <color name="azure">#007FFF</color> 
    <color name="blue">#0000FF</color> 
    <color name="violet">#7F00FF</color> 
    <color name="magenta">#FF00FF</color> 
<array name="rainbow"> 
    <item>@color/bright_pink</item> 
    <item>@color/red</item> 
    <item>@color/orange</item> 
    <item>@color/yellow</item> 
    <item>@color/chartreuse</item> 
    <item>@color/green</item> 
    <item>@color/spring_green</item> 
    <item>@color/cyan</item> 
    <item>@color/azure</item> 
    <item>@color/blue</item> 
    <item>@color/violet</item> 
    <item>@color/magenta</item> 
</array> 

和比該java代碼

String[] array = context.getResources().getStringArray(R.array.animals_array); String randomStr = array[new Random().nextInt(array.length)];

//here you define your layout

LinearLayout myLayout = (LinearLayout) findViewById(R.id.that_linear);

myLayout.setBackgroundColor(Color.parseColor(randomStr));

+0

@ Phil3992只是addded定義親愛的,檢查它。 – Kiloreux 2014-09-19 14:16:22

4

沒有什麼比提供的那些更好的答案。

如果你想要一個真正隨機顏色,從res文件中「隨機」選擇並沒有證明幾乎是健壯的。

相反,使用此代碼片段:

Random rnd = new Random(); 
    currentStrokeColor = Color.argb(255, rnd.nextInt(256), rnd.nextInt(256), rnd.nextInt(256)); 
+0

我得到價值-118692481300,超出整數? – kemdo 2017-11-20 05:26:08

0

可以使用ColorGenerator類來獲得隨機color.code。下面

ColorGenerator generator = ColorGenerator.MATERIAL; 

int color=generator.getRandomColor(); 

這裏

片段給你可以使用觀看基準設置顏色像

mUserName.setText("Suraj"); 

mUserName.setTextColor(color); //it will populate name with random color each time you open your activity 
+0

thanx用於更改格式Vrund Purohit – 2016-09-16 07:38:48