Android定义字符串数组资源并在程序中使用

来源:互联网 发布:注册一个软件工作室 编辑:程序博客网 时间:2024/04/30 04:03
在Android中可以使用字符串数组资源。

  在资源文件中定义方式如下:

  < ?xml version="1.0" encoding="utf-8"?>

  < resources>

  < string-array name="sa">

        <item>兑换</item>
        <item>积分排行</item>
        <item>积分详情</item>
        <item>我的兑换</item>
        <item>规则通知</item>

  < /string-array>

  < /resources>

  在Activity中取得方法如下:

  String[] sa = getResources().getStringArray(R.array.sa);

  还可以使用整数数组。

  定义名称为integer-array。

  调用方法为getIntArray(< id>)。

  使用方式同上。

 


那么在程序中如何获取咱们在配置文件中定义好的字符串数组呢?

Resources res = this.getResources(); //基本上获取在配置文件中的信息都要用到Resources,所以大家只需要查看下这个Resourece类得API文档基本都能找到自己所需要的东西
String[] arr = res.getStringArray(R.array.card_items);