android 读取array.xml 中String、int、drawable等数组及多维的方法

从array.xml 中读取String、int、drawable等数组/多维数组

xml数组定义:

   <array name="category_default">
        <item>@array/category_default_title</item>
        <item>@array/category_default_background</item>
        <item>@array/category_default_cover</item>
    </array>
    <array name="category_default_title">
        <item>@string/category_myshelf</item>
        <item>@string/category_myread</item>
        <item>@string/category_doctor</item>
        <item>@string/category_master</item>
        <item>@string/category_courseteam</item>
        <item>@string/category_userspace</item>
    </array>
    <array name="category_default_background">
        <item>@color/purple_af</item>
        <item>@color/green_8c</item>
        <item>@color/blue_59</item>
        <item>@color/pink_f6</item>
        <item>@color/yellow_fb</item>
        <item>@color/red_fc</item>
    </array>
    <array name="category_default_cover">
        <item>@drawable/category_myshelf_s</item>
        <item>@drawable/category_myread_s</item>
        <item>@drawable/category_doctor_s</item>
        <item>@drawable/category_doctor_s</item>
        <item>@drawable/category_courseteam_s</item>
        <item>@drawable/category_userspace_s</item>
    </array>

以上定义了二维数组“category_default”,和一维数组“category_default_title”、“category_default_background”、“category_default_cover”,数组中的内容在string、color和drawable中定义好了

读取数组:

private void init() {
  //obtain the referenced arrays
  TypedArray cateDefaultArray = getResources().obtainTypedArray(R.array.category_default);//读取二维数组
  String[] categoryTitleArray = getResources().getStringArray(cateDefaultArray.getResourceId(0, -1));//读取string数组
  int[] categoryBgArray = getResources().getIntArray(cateDefaultArray.getResourceId(1, -1));//读取int数组
  TypedArray categoryCoverArray = getResources().obtainTypedArray(cateDefaultArray.getResourceId(2, -1));//读取drawable数组
  cateDefaultArray.recycle();  
  ArrayList<Category> categoryList = new ArrayList<Category>();
  for (int i = 0; i < categoryTitleArray.length; i++) {
   Category temp = new Category();
   temp.setTitle(categoryTitleArray[i]);
   System.out.println("category:title="+temp.getTitle());
   temp.setColor(categoryBgArray[i]);
   System.out.println("category:color="+temp.getColor());
   temp.setId(categoryCoverArray.getResourceId(i, -1));
   System.out.println("category:id="+temp.getId());
   categoryList.add(temp);
  }
  categoryCoverArray.recycle();
 }

打印结果:

category:title=书架
category:color=-5271617
category:id=2130837668
category:title=论文
category:color=-7548558
category:id=2130837666
category:title=文献
category:color=-10897954
category:id=2130837663
category:title=资源
category:color=-624192
category:id=2130837663
category:title=团队
category:color=-276124
category:id=2130837661
category:title=空间
category:color=-237718
category:id=2130837670

以上是项目中的一个方法,绝对可用

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值