在xml中添加array

时间:2020-12-10 16:55:32

在values建立arrays(名字可自定义)的xml:

 <?xml version="1.0" encoding="utf-8"?>
<resources> <string-array name="mydata">
<item>hello</item>
<item>world</item>
</string-array> <integer-array name="mydata1">
<item>1</item>
<item>2</item>
</integer-array> </resources>

java中调用

 package com.example.array;

 import android.app.Activity;
import android.os.Bundle;
import android.util.Log; public class MainActivity extends Activity { @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main); String[] data=this.getResources().getStringArray(R.array.mydata);
Log.d("",data[0]);
Log.d("",data[1]); Log.d("",this.getResources().getStringArray(R.array.mydata)[0]); int[] data2=this.getResources().getIntArray(R.array.mydata1);
Log.d("",data2[0]+"");
} }