所以,基本上我有两个用XML定义的颜色整数数组。 第一个数组设置为Random。
int[] materialColors = getResources().getIntArray(R.array.materialColors);
int randomMaterialColor = materialColors[new Random().nextInt(materialColors.length)];
我希望应用程序的状态栏相应地更改为随机选择的颜色深色。
我使用了Switch case。
switch (randomMaterialColor){
case R.color.red:
window.setStatusBarColor(getResources().getColor(R.color.reds));
break;
case R.color.pink:
window.setStatusBarColor(getResources().getColor(R.color.pinks));
break;
case R.color.purple:
window.setStatusBarColor(getResources().getColor(R.color.purples));
break;
case R.color.deep_purple:
window.setStatusBarColor(getResources().getColor(R.color.deep_purple));
break;
case R.color.indigo:
window.setStatusBarColor(getResources().getColor(R.color.indigos));
break;
case R.color.blue:
window.setStatusBarColor(getResources().getColor(R.color.blues));
break;
case R.color.light_blue:
window.setStatusBarColor(getResources().getColor(R.color.light_blues));
break;
case R.color.cyan:
window.setStatusBarColor(getResources().getColor(R.color.cyans));
break;
case R.color.teal:
window.setStatusBarColor(getResources().getColor(R.color.teal));
break;
case R.color.green:
window.setStatusBarColor(getResources().getColor(R.color.greens));
break;
case R.color.light_green:
window.setStatusBarColor(getResources().getColor(R.color.light_greens));
break;
case R.color.lime:
window.setStatusBarColor(getResources().getColor(R.color.limes));
break;
case R.color.yellow:
window.setStatusBarColor(getResources().getColor(R.color.yellows));
break;
case R.color.amber:
window.setStatusBarColor(getResources().getColor(R.color.ambers));
break;
case R.color.deep_orange:
window.setStatusBarColor(getResources().getColor(R.color.deep_oranges));
break;
case R.color.brown:
window.setStatusBarColor(getResources().getColor(R.color.browns));
break;
case R.color.gray:
window.setStatusBarColor(getResources().getColor(R.color.grays));
break;
case R.color.blue_gray:
window.setStatusBarColor(getResources().getColor(R.color.blue_grays));
break;
default:
Toast.makeText(this, "Could not detect color!", Toast.LENGTH_SHORT);
}
应用程序可以运行,但状态栏颜色无法更改。 我无法在Switch案例中使用十六进制值。而randomMaterialColors是一个具有该值的Integer,但我无法找到解决方法。
我该如何解决这个问题?
修改[更新]
这是XML文件。 colors.xml,其中每种颜色的颜色值均为Hexa。这些来自Google的材料指南。
<?xml version="1.0" encoding="utf-8"?>
<resources>
<color name="red">#F44336</color>
<color name="pink">#E91E63</color>
<color name="purple">#9C27B0</color>
<color name="deep_purple">#673AB7</color>
<color name="indigo">#3F51B5</color>
<color name="blue">#2196F3</color>
<color name="light_blue">#03A9F4</color>
<color name="cyan">#00BCD4</color>
<color name="teal">#009688</color>
<color name="green">#4CAF50</color>
<color name="light_green">#8BC34A</color>
<color name="lime">#CDDC39</color>
<color name="yellow">#FFEB3B</color>
<color name="amber">#FF9800</color>
<color name="deep_orange">#FF5722</color>
<color name="brown">#795548</color>
<color name="gray">#9E9E9E</color>
<color name="blue_gray">#607D8B</color>
<string-array name="materialColors">
<item>@color/red</item>
<item>@color/pink</item>
<item>@color/purple</item>
<item>@color/deep_purple</item>
<item>@color/indigo</item>
<item>@color/blue</item>
<item>@color/light_blue</item>
<item>@color/cyan</item>
<item>@color/teal</item>
<item>@color/green</item>
<item>@color/light_green</item>
<item>@color/lime</item>
<item>@color/yellow</item>
<item>@color/amber</item>
<item>@color/deep_orange</item>
<item>@color/brown</item>
<item>@color/gray</item>
<item>@color/blue_gray</item>
</string-array>
</resources>
答案 0 :(得分:0)
根据文档,您还需要设置几个标志,而不是设置颜色。请尝试以下代码。
Window window = activity.getWindow();
window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
window.clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
window.setStatusBarColor(activity.getResources().getColor(R.color.example_color));
设置这些标志。这应该有用。
[编辑]更新
所以这就是问题所在。数字 grandomMaterialColor 是整数值,您将该整数值与 R.color.red 进行比较,整数例如 21453698789 的值,永远不会满足的情况。
为此,你可以做一件事。由于您已经在XML中定义了颜色列表,并且您知道该数组中值的确切位置。您可以按如下方式修改开关盒。
int[] materialColors = getResources().getIntArray(R.array.rainbow);
Random ranom=new Random();
int randomMaterialColor=ranom.nextInt(materialColors.length-1);
switch (randomMaterialColor){
case 0:
window.setStatusBarColor(getResources().getColor(R.color.reds));
break;
case 1:
window.setStatusBarColor(getResources().getColor(R.color.pinks));
break;
case 2:
window.setStatusBarColor(getResources().getColor(R.color.purples));
break;
case 3:
window.setStatusBarColor(getResources().getColor(R.color.deep_purple));
break;
case 4:
window.setStatusBarColor(getResources().getColor(R.color.indigos));
break;
case 5:
window.setStatusBarColor(getResources().getColor(R.color.blues));
break;
case 6:
window.setStatusBarColor(getResources().getColor(R.color.light_blues));
break;
case 7:
window.setStatusBarColor(getResources().getColor(R.color.cyans));
break;
case 8:
window.setStatusBarColor(getResources().getColor(R.color.teal));
break;
case 9:
window.setStatusBarColor(getResources().getColor(R.color.greens));
break;
case 10:
window.setStatusBarColor(getResources().getColor(R.color.light_greens));
break;
case 11:
window.setStatusBarColor(getResources().getColor(R.color.limes));
break;
case 12:
window.setStatusBarColor(getResources().getColor(R.color.yellows));
break;
case 13:
window.setStatusBarColor(getResources().getColor(R.color.ambers));
break;
case 14:
window.setStatusBarColor(getResources().getColor(R.color.deep_oranges));
break;
case 15:
window.setStatusBarColor(getResources().getColor(R.color.browns));
break;
case 16:
window.setStatusBarColor(getResources().getColor(R.color.grays));
break;
case 17:
window.setStatusBarColor(getResources().getColor(R.color.blue_grays));
break;
default:
Toast.makeText(this, "Could not detect color!", Toast.LENGTH_SHORT);
}
答案 1 :(得分:0)
解析颜色值,如
Color.parseColor(myColor);//myColor in hexadecimal.
返回整数值。
答案 2 :(得分:0)
您可以尝试其他选项。创建一个包含所有颜色名称的附加String数组,如红色,粉红色,紫色等。然后你从数组中得到一个随机颜色名称:
String[] materialColorNames = getResources().getStringArray(R.array.materialColorNames);
String randomMaterialColorName = materialColors[new Random().nextInt(
materialColorNames.length)];
现在,由于您的状态栏颜色以“s”结尾,因此您可以简单地执行此操作而不是整个开关案例:
int randomStatusBarColorId = getResources().getIdentifier(
randomMaterialColorName + "s", "color", getPackageName());
windows.setStatusBarColor(getResources().getColor(randomStatusBarColorId));