数组资源(arrays)的使用

来源:互联网 发布:windows控制台程序作用 编辑:程序博客网 时间:2024/06/02 05:30

Android的资源布局类型表:

 

下面通过一个实例演示数组资源的使用,将数组资源的内容显示在界面上:

Activity:

[java] view plaincopy
  1. package com.lovo.activity;  
  2.   
  3. import android.app.Activity;  
  4. import android.content.res.TypedArray;  
  5. import android.os.Bundle;  
  6. import android.util.Log;  
  7. import android.widget.TextView;  
  8.   
  9. public class TestArrayActivity extends Activity {  
  10.   
  11.     @Override  
  12.     protected void onCreate(Bundle savedInstanceState) {  
  13.         super.onCreate(savedInstanceState);  
  14.         setContentView(R.layout.testarray);  
  15.         // 得到TextView实例  
  16.         TextView tx = (TextView) findViewById(R.id.tx);  
  17.         // 得到字符串数组  
  18.         String[] strAry = getResources().getStringArray(R.array.str_ary);  
  19.         String str = "";  
  20.         // 得到整型数组  
  21.         int[] intAry = getResources().getIntArray(R.array.int_ary);  
  22.         // 得到普通数组  
  23.         TypedArray dateAry = getResources().obtainTypedArray(R.array.date);  
  24.         for (int i = 0; i < intAry.length; i++) {  
  25.             Log.i("intAry:", intAry[i] + "");// 打印到LogCat  
  26.             str += intAry[i] + "   ";  
  27.         }  
  28.         str += "\n";  
  29.         for (int i = 0; i < strAry.length; i++) {  
  30.             Log.i("strAry:", strAry[i]);  
  31.             str += strAry[i] + "  ";  
  32.         }  
  33.         str += "\n";  
  34.         for (int i = 0; i < dateAry.length(); i++) {  
  35.             Log.i("dateAry:", dateAry.getString(i));  
  36.             str += dateAry.getString(i) + "  ";  
  37.         }  
  38.         // 将数组中的元素设置到TextView中显示出来  
  39.         tx.setText(str);  
  40.     }  
  41. }  


 


布局XML,testarray.xml:

[html] view plaincopy
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     android:layout_width="match_parent"  
  4.     android:layout_height="match_parent"  
  5.     android:orientation="vertical" >  
  6.   
  7.     <TextView  
  8.         android:id="@+id/tx"  
  9.         android:layout_width="match_parent"  
  10.         android:layout_height="wrap_content" />  
  11.   
  12. </LinearLayout>  


数组XML:

[html] view plaincopy
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <resources>  
  3.   
  4.     <string-array name="str_ary">  
  5.         <item>张三</item>  
  6.         <item>李四</item>  
  7.         <item>王五</item>  
  8.     </string-array>  
  9.   
  10.     <integer-array name="int_ary">  
  11.         <item>1</item>  
  12.         <item>2</item>  
  13.         <item>3</item>  
  14.     </integer-array>  
  15.   
  16.     <array name="date">  
  17.         <item>昨天</item>  
  18.         <item>今天</item>  
  19.         <item>明天</item>  
  20.     </array>  
  21.   
  22. </resources>  


//-------------------------------------------------------自己-------------------------------------------------------
总结:自己使用到了,string-array,注意提的就是如果想要加入空格,就需要&#160;
    大概一个汉字的距离,需要四个&#160;,才可以。

0 0
原创粉丝点击