数组(Array)资源

      上面的程序中在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所示的结果。

 

  

原文地址:https://www.cnblogs.com/wolipengbo/p/3437808.html