数组
(1)使用字符串数组
在values下新建XML文件arrays
数组分为两种:integer-array(整型数组) 和string-array(字符串数组),这里主要学习字符串数组。
在arrays里面写入如下标记
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string-array name="arr">
<item>Hello</item>
<item >eoe</item>
</string-array>
</resources>
再在主程序里定义字符串型数组,并给它赋值,输出它。
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
String []arr=getResources().getStringArray(R.array.arr);
for(String string : arr){
System.out.println(string);
}
}
可以在LogCat中看到hello和eoe被成功输出。
(2)使用数组资源
主布局使用线性布局、垂直布局,主程序不变,主布局代码如下
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".MainActivity" >
<ListView
android:entries="@array/arr"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
</ListView>
</RelativeLayout>
效果如下