定义数组资源的xml文件位于/res/values目录下,文件名为arrays.xml
数组共有三种类型:<array></array>、<string-array></string-array>、<integer-array></integer-array>
访问方式:R.array.array_name
示例:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<array name="colors">
<item>@color/purple_200</item>
<item>@color/black</item>
<item>@color/teal_200</item>
</array>
<string-array name="fruits">
<item>orange</item>
<item>apple</item>
<item>grape</item>
</string-array>
<integer-array name="sizes">
<item>10</item>
<item>20</item>
<item>30</item>
</integer-array>
</resources>
activity中获得数组
val colors = resources.obtainTypedArray(R.array.colors)
val fruits = resources.getStringArray(R.array.fruits)
val sizes = resources.getIntArray(R.array.sizes)