如何从XML格式化String数组

时间:2016-07-10 20:41:56

标签: android arrays xml android-resources

我想在这里为 String 资源做同样的事情:

write(String.format(getString(R.string.night), "Scurcola"));

但对于 String Array ,定义如下:

<string-array name="goodMorning" formated="false">
    <item>Good Morning folks!</item>
    <item>Sun\'s out, guns out.</item>
    <item>Careless vampires burn, and you wake up!</item>
    <item>Can\'t find the moon. Oh it\'s probably morning then..</item>
    <item>Darkness flees from %s, wake up!</item>
    <item>Sunlight manifests, it\'s morning %s!</item>
    <item>Stars went to bed, and %s wakes up!</item>
    <item>Be beep. Be beep. %s wake up!</item>
</string-array>

我怎样才能做到这一点?

2 个答案:

答案 0 :(得分:1)

我解决了这个问题,添加了编辑def iterate(m): for row in range(0,4): for col in range(0,4): print(m[row-1][col+1]+ m[row][col+1]+ m[row+1][col-1]+ m[row+1][col]+ m[row+1][col+1]) mat=[ ["A","B","C","D",""], ["E","F","G","H",""], ["I","J","K","L",""], ["M","N","O","P",""], ["", "", "", "", ""] ] iterate(mat)

的方法
TextView

这样我只需输入:

public void write(int id, int position, String text){
    String[] msg = getResources().getStringArray(id);
    messages.add(String.format(msg[position], text));
    adapter = new ArrayAdapter<>(this, android.R.layout.simple_list_item_1, messages);
    screen.setAdapter(adapter);
}

输出 write(R.array.goodMorning, randInt(0, 7), "Scurcola"); item随机,因此格式化与&#34; Scurcola &#34; String Array我以String传递。

答案 1 :(得分:0)

每个数组项本身实际上都是一个字符串 因此,每个项目都可以单独格式化。

即:

write(String.format(getResources().getStringArray(R.array.goodMorning)[0], "Scurcola"));