Android学习笔记数组资源文件

在android中我们可以通过数组资源文件,定义数组元素。

数组资源文件是位于values目录下的

array.xml

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <string-array name="listitem">
        <item>活着就是为了被世界改变</item>
        <item>机遇很重要</item>
        <item>求知若饥,虚怀若谷</item>
    </string-array>
</resources>

这样我们就定义了个字符串数组。

需要引用的话也很方便直接在xml中引用就可以了
activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">
    <ListView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:entries="@array/listitem"
        android:id="@+id/listView"/>
</RelativeLayout>

在Activty中获取该数组资源用下面一串代码就可以了

  String[] arr = getResources().getStringArray(R.array. listitem);
原文地址:https://www.cnblogs.com/lzpq/p/12850278.html