如何列出数组中的Android字符串资源

时间:2014-04-27 09:06:38

标签: android

我想按照Reference drawable from string-array in android

中的说明做一些类似的事情
private Integer[] mThumbIds = {
        R.drawable.sample_2, R.drawable.sample_3,
        R.drawable.sample_4, R.drawable.sample_5,
        R.drawable.sample_6, R.drawable.sample_7,
        R.drawable.sample_0, R.drawable.sample_1,
        R.drawable.sample_2, R.drawable.sample_3,
        R.drawable.sample_4, R.drawable.sample_5,
        R.drawable.sample_6, R.drawable.sample_7,
        R.drawable.sample_0, R.drawable.sample_1,
        R.drawable.sample_2, R.drawable.sample_3,
        R.drawable.sample_4, R.drawable.sample_5,
        R.drawable.sample_6, R.drawable.sample_7
};

我试过这个.java文件:

    int[] descriptions = new int[] { 
        R.string.description_section1, 
        R.string.description_section2, 

    }; 

但它似乎不是正确的语法。

2 个答案:

答案 0 :(得分:2)

按照docs

中的说明尝试此操作
int[] descriptions = { 
    R.string.description_section1, 
    R.string.description_section2, 

}; 

答案 1 :(得分:2)

在array.xml中定义数组:

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

    <string-array name="descriptions">
        <item>section1</item>
        <item>section1</item>
    </string-array>
 </resources>

然后试试这个:

String[] descriptions = getResources().getStringArray(
                R.array.descriptions);