上面的程序中在Java代码中定义了两个数组,Android并不推荐在Java代码中定义数组,因为Androd允许通过资源文件来定义数组资源。

Android采用位于/res/values目录下的arrays.xml文件来定义数组,定义数组时XML资源文件的根元素也是<resources.../>元素,该元素内可包含如下三种子元素。

  • <array.../>子元素:定义普通类型的数组。例如Drawable数组。
  • <string-array.../>子元素:定义字符串数组。
  • <integer-array.../>子元素:定义整数数组。

一旦在资源文件中定义了数组资源,接下来就可以在Java文件中通过如下形式来访问资源了:

[<package_name>.]R.array.array_name

在XML代码中则可通过如下形式进行访问:

@[<package_name>:]array/array_name

为了能在Java程序访问都实际数组,Resources提供了如下方法。

  • String[] getStringArray(int id):根据资源文件中字符串数组资源的名称来获取实际的字符串数组。
  • int[] getIntArray(int id):根据资源文件中整型数组资源的名称来获取实际的整型数组。
  • TypeArray obtainTypedArray(int id):根据资源文件中普通数组资源的名称来获取实际的普通数组。

TypedArray代表一个通用类型的数组,该类提供了getXxx(int index)方法来获取指定索引处的数组元素。

下面为该应用程序增加如下数组资源文件。

<?xml version="1.0" encoding="utf-8"?>
<resources>
<!-- 定义一个Drawable数组 -->
<array name="plain_arr">
<item>@color/c1</item>
<item>@color/c2</item>
<item>@color/c3</item>
<item>@color/c4</item>
<item>@color/c5</item>
<item>@color/c6</item>
<item>@color/c7</item>
<item>@color/c8</item>
<item>@color/c9</item>
</array>
<!-- 定义字符串数组 -->
<string-array name="string_arr">
<item>@string/c1</item>
<item>@string/c2</item>
<item>@string/c3</item>
<item>@string/c4</item>
<item>@string/c5</item>
<item>@string/c6</item>
<item>@string/c7</item>
<item>@string/c8</item>
<item>@string/c9</item>
</string-array>
<!-- 定义字符串数组 -->
<string-array name="books">
<item>疯狂Java讲义</item>
<item>疯狂Ajax讲义</item>
<item>疯狂Android讲义</item>
</string-array>
</resources>

定义了上面的数组资源之后,既可在XML文件中使用这些资源,也可在Java程序中使用这些资源。例如如下界面布局文件中定义一个ListView数组,并将android:entries属性值指定为一个数组。界面布局文件代码如下。

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
android:gravity="center_horizontal"
>
<!-- 使用字符串资源,尺寸资源 -->
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/app_name"
android:gravity="center"
android:textSize="@dimen/title_font_size" />
<!-- 定义一个GridView组件,使用尺寸资源中定义的长度来指定水平间距、垂直间距 -->
<GridView android:id="@+id/grid01"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:horizontalSpacing="@dimen/spacing"
android:verticalSpacing="@dimen/spacing"
android:numColumns="3"
android:gravity="center"></GridView>
<!-- 定义ListView组价,使用了数组资源 -->
<ListView android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:entries="@array/books"
></ListView>
</LinearLayout>

接下来程序中无须定义数组,程序直接使用资源文件中定义的数组。程序代码如下。

package com.example.studyresources;

import android.os.Bundle;
import android.app.Activity;
import android.content.res.Resources;
import android.content.res.TypedArray;
import android.view.Menu;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.GridView;
import android.widget.TextView; public class ArrayResTest extends Activity { //获取系统定义的数组资源
String[] texts;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_array_res_test);
texts=getResources().getStringArray(R.array.string_arr);
//创建一个BaseAdapter对象
BaseAdapter ba=new BaseAdapter()
{ @Override
public int getCount() {
// TODO Auto-generated method stub
//指定一共包含9个选型
return texts.length;
} @Override
public Object getItem(int position) {
// TODO Auto-generated method stub
//返回指定位置的文本
return texts[position];
} @Override
public long getItemId(int position) {
// TODO Auto-generated method stub
return position;
}
//重写该方法,该方法返回的View将作为GridView的每个格子
@Override
public View getView(int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
TextView text=new TextView(ArrayResTest.this);
Resources res=ArrayResTest.this.getResources();
//使用尺度资源来设置文本框的高度、宽度
text.setWidth((int)res.getDimension(R.dimen.cell_width));
text.setHeight((int)res.getDimension(R.dimen.cell_height));
//使用字符串资源设置文本框的内容
text.setText(texts[position]);
TypedArray icons=res.obtainTypedArray(R.array.plain_arr);
//使用颜色资源来设置文本框的背景色
text.setBackgroundDrawable(icons.getDrawable(position));
text.setTextSize(20);
return text;
}
}; GridView grid=(GridView)findViewById(R.id.grid01);
//为GridView设置Adapter
grid.setAdapter(ba);
} @Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.array_res_test, menu);
return true;
} }

上面的程序中粗体字代码就是使用数组资源的关键代码。运行上面的程序将看到如图6.2所示的结果。

数组(Array)资源的更多相关文章

  1. Android使用xml文件中的array资源

    Android中有种使用数组的非常简单的用法,在xml文件中获取. 创建数组资源 在value目录下创建arrays.xml文件 然后在arrays.xml文件中使用<string-array& ...

  2. Java ArrayList和Vector、LinkedList与ArrayList、数组(Array)和列表集合(ArrayList)的区别

    ArrayList和Vector的区别ArrayList与Vector主要从二方面来说.  一.同步性:   Vector是线程安全的,也就是说是同步的,而ArrayList是线程序不安全的,不是同步 ...

  3. go 数组(array)、切片(slice)、map、结构体(struct)

    一 数组(array) go语言中的数组是固定长度的.使用前必须指定数组长度. go语言中数组是值类型.如果将数组赋值给另一个数组或者方法中参数使用都是复制一份,方法中使用可以使用指针传递地址. 声明 ...

  4. javascript类型系统——数组array

    × 目录 [1]创建 [2]本质 [3]稀疏[4]长度[5]遍历[6]类数组 前面的话 除了对象之外,数组Array类型可能是javascript中最常用的类型了.而且,javascript中的数组与 ...

  5. swift基本用法-数组array

    数组简单用法 //------------------------------------------------------------------------------ // 1. 数组定义 / ...

  6. C#中数组Array、ArrayList、泛型List<T>的比较

    在C#中数组Array,ArrayList,泛型List都能够存储一组对象,但是在开发中根本不知道用哪个性能最高,下面我们慢慢分析分析. 一.数组Array 数组是一个存储相同类型元素的固定大小的顺序 ...

  7. Javascript基础系列之(四)数据类型 (数组 array)

    字符串,数值,布尔值都属于离散值(scalar),如果某个变量是离散的,那么任何时候它只有一个值. 如果想使用变量存储一组值,就需要使用数组(array). 数组是由多个名称相同的树值构成的集合,集合 ...

  8. AS3 - 数组Array的几个常用方法(附样例)

    AS3 - 数组Array的几个常用方法(附样例) 2015-03-30 10:39发布:hangge浏览:241   Flex/Flash开发中,经常会使用到数组,下面总结了一些数组的常用方法. 1 ...

  9. Linux数组array基础

    Linux数组array基础[${a[*]}和$a的区别] Bash中,数组变量的赋值有两种方法: (1) name = (value1 ... valuen) 此时下标从0开始 (2) name[i ...

  10. 学习Swift -- 数组(Array) - 持续更新

    集合类型--数组 Array是Swift中的一种集合类型:数组,数组是使用有序列表储存同一类型的多个值,与OC的NSArray的最大不同是,Swift的数组是值类型,OC的数组是引用类型 声明数组的方 ...

随机推荐

  1. js动态添加file控件

    <html></head><script language="javascript" type="text/ecmascript" ...

  2. PAT (Advanced Level) 1092. To Buy or Not to Buy (20)

    简单题. #include<cstdio> #include<cstring> ; char s1[maxn],s2[maxn]; ]; ]; int main() { sca ...

  3. USACO Section 1.3 Prime Cryptarithm 解题报告

    题目 题目描述 牛式的定义,我们首先需要看下面这个算式结构: * * * x * * ------- * * * <-- partial product 1 * * * <-- parti ...

  4. html5中的postMessage解决跨域问题

    解决跨域问题的方法有很多,如:图像ping(简单).jsonp(缺点是不能实现跨域post).CROS(CORS的本质让服务器通过新增响应头Access-Control-Allow-Origin,通过 ...

  5. 一个java 开源神经网络引擎 joone

    https://sourceforge.net/projects/joone/files/?source=navbar joone

  6. my97datepicker开始日期小于结束日期格式化时间精确届时分秒

    my97datepicker开始日期小于结束日期格式化时间精确到时分秒 一 , 需求: 结束时间 > 开始时间, 不符合的时间段不能选择.比如我选择开始日期是7月28,那结束的日期将只能从7月2 ...

  7. Freemodbus 1.5

    源:http://blog.sina.com.cn/s/blog_4935209001012eax.html 网站位置:http://www.freemodbus.org/index.php?lang ...

  8. Linux中通过命令直接删除文件中最后一行

    何谓Sed(Stream EDitor):Sed原为UNIX系统上的非交谈式文字编辑器(non-interactive stream editor).当Sed读入待编辑文件,会依编辑命令来进行文件的编 ...

  9. Lumen 时区设置

    根据 Laravel 4.x 和 5.0 的经验, 只需要到 config/app.php 中设置下 'timezone' 参数为 'PRC' 就好了, 找到 Lumen 的 config 目录, 在 ...

  10. MYSQL同步--主从同步问题集锦

    1  同步停止,报错误:  Could not find first log file name in binary log index file 数据库主从出错: Slave_IO_Running: ...