Android资源访问——数组

来源:互联网 发布:驾驶证生成器软件 编辑:程序博客网 时间:2024/06/14 18:09

数组
(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>

效果如下
这里写图片描述

0 0
原创粉丝点击