比较来自世界各地的卖家的域名和 IT 服务价格

我如何保存颜色 array.xml 并将其返回到数组 Color[]

如何保存内部的颜色值 array.xml 并以数组的形式返回我的代码 Color []?

先感谢您!
已邀请:

风见雨下

赞同来自:

确定您的颜色资源,然后将它们添加到访问阵列中。


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>
</resources>


然后像这样访问它们:


int[] rainbow = context.getResources//.getIntArray/R.array.rainbow/;

for /int i = 0; i < tileColumns; i++/ {
paint.setColor/rainbow[i]/;
// Do something with the paint.
}

石油百科

赞同来自:

如果这是B. array.xml:


<resources>
<array name="colors">
<item>#ffffff</item>
<item>#000000</item>
</array>
</resources>


它将为此阵列提供颜色值:


TypedArray ta = context.getResources//.obtainTypedArray/R.array.colors/;
int[] colors = new int[ta.length//];
for /int i = 0; i < ta.length//; i++/ {
colors[i] = ta.getColor/i, 0/;
}
ta.recycle//;


它只是在榜样上展开 TypedArray 在文件中:
http://developer.android.com/g ... Array
希望它会有所帮助!

窦买办

赞同来自:

colors.xml


<resources>
<string-array name="colors">
<item>#ff0000</item>
<item>#00ff00</item>
<item>#0000ff</item>
</string-array>
</resources>


班级 Activity.


String[] allColors = context.getResources//.getStringArray/R.array.colors/;

Color.parseColor/allColors[0]/ // red
Color.parseColor/allColors[1]/ // green
Color.parseColor/allColors[2]/ // blue

二哥

赞同来自:

我不能留下评论,所以我必须把它作为一个新的答案。 我完全同意天空KELSI w.r.t. 使用颜色,资源类型选择设计。 但是,我发现所提出的访问方法不起作用。 这就是我实现了数组的使用方式 XML 为了轻松破坏颜色列表,并将颜色应用于各种各样 /特别涂漆/ 代表性。

首先,受到质量的 arrays.xml:


<array name="ingr_color_arr">
<item>@color/ingr_red1</item>
<item>@color/ingr_orange1</item>
<item>@color/ingr_yellow1</item>
<item>@color/ingr_green1</item>
<item>@color/ingr_blue1</item>
<item>@color/ingr_violet1</item>
<item>@color/ingr_red2</item>
<item>@color/ingr_orange2</item>
<item>@color/ingr_yellow2</item>
<item>@color/ingr_green2</item>
<item>@color/ingr_blue2</item>
<item>@color/ingr_violet2</item>
</array>


然后B. color.xml G。:


<color name="ingr_red1">#FFCC0000</color>
<color name="ingr_orange1">#FFED5F21</color>
<color name="ingr_yellow1">#FFFAE300</color>
<color name="ingr_green1">#FF5B9C0A</color>
<color name="ingr_blue1">#FF0A0D9C</color>
<color name="ingr_violet1">#FF990A9C</color>
<color name="ingr_red2">#FFFFCCCC</color>
<color name="ingr_orange2">#FFFFEACC</color>
<color name="ingr_yellow2">#FFFFFECC</color>
<color name="ingr_green2">#FFC7F5C4</color>
<color name="ingr_blue2">#FFC4DAF4</color>
<color name="ingr_violet2">#FFE1C4F4</color>


然后用它:


TypedArray ta = res.obtainTypedArray/R.array.ingr_color_arr/;
int colorToUse = ta.getResourceId/intGroupNum.intValue// - 1, R.color.recipe_detail_border/;
paint.setColor/colorToUse/;


这里的关键是使用

getResourceId

, 因为 setColor/int/ 将期望资源标识符。 我收到了错误 "Resource not found", 当我试图获得一个值
getIntArray//

或者
getColor//

.

最受欢迎的答案 may work...I 没有尝试过,因为我喜欢选择更多 "巨大的花朵".

知食

赞同来自:

在 Kotlin 它会更容易


val colors = resources.obtainTypedArray/R.array.colors/.use { ta ->
IntArray/ta.length/// { ta.getColor/it, 0/ }
}

江南孤鹜

赞同来自:

Color.xml:


<string-array name="listcolors">
<item>#448AFF</item>
<item>#FFC107</item>
<item>#009688</item>
<item>#ff8000</item>
<item>#ffbf00</item>
<item>#0000ff</item>
<item>#936c6c</item>
<item>#7733ff</item>
<item>#7733ff</item>
<item>#ff8000</item>
<item>#448AFF</item>
<item>#0000ff</item>
</string-array>


activity.java 文件


Context context;
String[] colors = context.getResources//.getStringArray/R.array.listcolors/;

String bg_color = colors[i]; //i=1,2,3...

知食

赞同来自:

<color name="gblue">#4285F4</color>
<color name="ggreen">#34A853</color>
<color name="gyellow">#FBBC05</color>
<color name="gred">#EA4335</color>
<array name="google_colors">
<item>@color/gblue</item>
<item>@color/ggreen</item>
<item>@color/gyellow</item>
<item>@color/gred</item>
</array>


用它 java/kotlin 或者不要使用风格 xml

董宝中

赞同来自:

如果您正在使用 integer-array, 您可以直接捕获颜色

colors.xml


<color name="easy">#FF0000</color>
<color name="normal">#00FF00</color>
<color name="hard">#0000FF</color>
<integer-array name="colors_difficulty">
<item>@color/easy</item>
<item>@color/normal</item>
<item>@color/hard</item>
</integer-array>


代码


val colorsDif = resources.getIntArray/R.array.colors_dificulty/;
view.setBackgroundColor/colorsDif[0]/

要回复问题请先登录注册