Android 字符串?dāng)?shù)組資源

2018-02-18 11:54 更新

你可以在/res/values子目錄下的任何文件中將字符串?dāng)?shù)組指定為資源。

例子

你可以使用名為string-array的XML節(jié)點(diǎn)。此節(jié)點(diǎn)是資源的子節(jié)點(diǎn)。

以下代碼顯示資源文件中的數(shù)組示例。

<?xml version="1.0" encoding="utf-8"?>
<resources>
      <string name="hello">hello</string>
      <string name="app_name">hello appname</string>
      <string-array name="test_array">
                 <item>one</item>
                 <item>two</item>
                 <item>three</item>
      </string-array>
</resources>

一旦你有這個(gè)字符串?dāng)?shù)組資源定義,你可以在Java代碼中檢索這個(gè)數(shù)組,如下。

// www.o2fo.com
//Get access to Resources object from an Activity
Resources res = your_Activity.getResources();
String strings[] = res.getStringArray(R.array.test_array);

//Print strings
for (String s: strings)
{
    Log.d("example", s);
}


以上內(nèi)容是否對(duì)您有幫助:
在線筆記
App下載
App下載

掃描二維碼

下載編程獅App

公眾號(hào)
微信公眾號(hào)

編程獅公眾號(hào)