初学Android,字符串,数字,尺寸,数组资源(十二) .

下面是使用字符串,数字,尺寸,数组资源的例子,挺好,一个例子已经囊括了不少Android资源的使用方法

arrays.xml

 

  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <resources>  
  3.     <array name = "plain_arr">  
  4.         <item>@color/c1</item>  
  5.         <item>@color/c2</item>  
  6.         <item>@color/c3</item>  
  7.         <item>@color/c4</item>  
  8.         <item>@color/c5</item>  
  9.         <item>@color/c6</item>  
  10.         <item>@color/c7</item>  
  11.         <item>@color/c8</item>  
  12.         <item>@color/c9</item>  
  13.     </array>  
  14.     <string-array name = "string_arr">  
  15.         <item >@string/c1</item>  
  16.         <item >@string/c2</item>  
  17.         <item >@string/c3</item>  
  18.         <item >@string/c4</item>  
  19.         <item >@string/c5</item>  
  20.         <item >@string/c6</item>  
  21.         <item >@string/c7</item>  
  22.         <item >@string/c8</item>  
  23.         <item >@string/c9</item>  
  24.     </string-array>  
  25.     <string-array name = "books">  
  26.         <item >Java</item>  
  27.         <item >Ajax</item>  
  28.         <item >Android</item>  
  29.     </string-array>  
  30. </resources>  
<?xml version="1.0" encoding="utf-8"?>
<resources>
    <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>

colors.xml

 

 

  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <resources>  
  3.     <color name="c1">#F00</color>  
  4.     <color name="c2">#0F0</color>  
  5.     <color name="c3">#00F</color>  
  6.     <color name="c4">#0FF</color>  
  7.     <color name="c5">#F0F</color>  
  8.     <color name="c6">#FF0</color>  
  9.     <color name="c7">#07F</color>  
  10.     <color name="c8">#70F</color>  
  11.     <color name="c9">#F70</color>  
  12. </resources>  
<?xml version="1.0" encoding="utf-8"?>
<resources>
    <color name="c1">#F00</color>
    <color name="c2">#0F0</color>
    <color name="c3">#00F</color>
    <color name="c4">#0FF</color>
    <color name="c5">#F0F</color>
    <color name="c6">#FF0</color>
    <color name="c7">#07F</color>
    <color name="c8">#70F</color>
    <color name="c9">#F70</color>
</resources>

dimens.xml

  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <resources>  
  3.     <dimen name="spacing">8dp</dimen>  
  4.     <dimen name="cell_width">60dp</dimen>  
  5.     <dimen name="cell_height">66dp</dimen>  
  6.     <dimen name="title_font_size">18sp</dimen>  
  7. </resources>  
<?xml version="1.0" encoding="utf-8"?>
<resources>
    <dimen name="spacing">8dp</dimen>
    <dimen name="cell_width">60dp</dimen>
    <dimen name="cell_height">66dp</dimen>
    <dimen name="title_font_size">18sp</dimen>
</resources>

strings.xml

 

 

  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <resources>  
  3.     <string name="hello">Hello World, ValuesResTest!</string>  
  4.     <string name="app_name">字符串,数字,尺寸,数组资源</string>  
  5.     <string name="c1">F00</string>  
  6.     <string name="c2">0F0</string>  
  7.     <string name="c3">00F</string>  
  8.     <string name="c4">0FF</string>  
  9.     <string name="c5">F0F</string>  
  10.     <string name="c6">FF0</string>  
  11.     <string name="c7">07F</string>  
  12.     <string name="c8">70F</string>  
  13.     <string name="c9">F70</string>  
  14. </resources>  
<?xml version="1.0" encoding="utf-8"?>
<resources>
    <string name="hello">Hello World, ValuesResTest!</string>
    <string name="app_name">字符串,数字,尺寸,数组资源</string>
    <string name="c1">F00</string>
    <string name="c2">0F0</string>
    <string name="c3">00F</string>
    <string name="c4">0FF</string>
    <string name="c5">F0F</string>
    <string name="c6">FF0</string>
    <string name="c7">07F</string>
    <string name="c8">70F</string>
    <string name="c9">F70</string>
</resources>


主界面xml

 

 

  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     android:layout_width="fill_parent"  
  4.     android:layout_height="fill_parent"  
  5.     android:orientation="vertical" >  
  6.   
  7.     <TextView  
  8.         android:layout_width="wrap_content"  
  9.         android:layout_height="wrap_content"  
  10.         android:text="@string/app_name"  
  11.         android:gravity="center"  
  12.         android:textSize="@dimen/title_font_size"  
  13.          />  
  14.   
  15.     <GridView  
  16.         android:id="@+id/grid01"  
  17.         android:layout_width="wrap_content"  
  18.         android:layout_height="wrap_content"  
  19.         android:horizontalSpacing="@dimen/spacing"  
  20.         android:verticalSpacing="@dimen/spacing"  
  21.         android:numColumns="3"   
  22.         android:gravity="center">  
  23.     </GridView>  
  24.   
  25.     <ListView  
  26.         android:id="@+id/listView1"  
  27.         android:layout_width="wrap_content"  
  28.         android:layout_height="wrap_content"   
  29.         android:entries="@array/books"  
  30.         >  
  31.     </ListView>  
  32.   
  33. </LinearLayout>  
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >

    <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
        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
        android:id="@+id/listView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" 
        android:entries="@array/books"
        >
    </ListView>

</LinearLayout>


代码

 

 

  1. package WangLi.Resources.Values;  
  2.   
  3. import android.app.Activity;  
  4. import android.content.res.Resources;  
  5. import android.content.res.TypedArray;  
  6. import android.os.Bundle;  
  7. import android.view.View;  
  8. import android.view.ViewGroup;  
  9. import android.widget.BaseAdapter;  
  10. import android.widget.GridView;  
  11. import android.widget.TextView;  
  12.   
  13. public class ValuesResTest extends Activity {  
  14.     /** Called when the activity is first created. */  
  15.     String[] texts;  
  16.     @Override  
  17.     public void onCreate(Bundle savedInstanceState) {  
  18.         super.onCreate(savedInstanceState);  
  19.         setContentView(R.layout.main);  
  20.         texts = getResources().getStringArray(R.array.string_arr);  
  21.         //创建一个BaseAdapter对象   
  22.         BaseAdapter ba = new BaseAdapter()  
  23.         {  
  24.             public int getCount()  
  25.             {  
  26.                 return texts.length;  
  27.             }  
  28.               
  29.             public Object getItem(int position)  
  30.             {  
  31.                 return texts[position];  
  32.             }  
  33.               
  34.             public long getItemId(int position)  
  35.             {  
  36.                 return position;  
  37.             }  
  38.               
  39.             public View getView(int position,View convertView,ViewGroup parent)  
  40.             {  
  41.                 TextView text = new TextView(ValuesResTest.this);  
  42.                 Resources res = ValuesResTest.this.getResources();  
  43.                 //使用尺寸资源来设置文本框的高度,宽度   
  44.                 text.setWidth((int)res.getDimension(R.dimen.cell_width));  
  45.                 text.setHeight((int)res.getDimension(R.dimen.cell_height));  
  46.                 //使用字符串资源设置文本框的内容   
  47.                 text.setText(texts[position]);  
  48.                 TypedArray icons = res.obtainTypedArray(R.array.plain_arr);  
  49.                 //使用颜色资源来设置文本框的背景色   
  50.                 text.setBackgroundDrawable(icons.getDrawable(position));  
  51.                 text.setTextSize(20);  
  52.                 return text;  
  53.             }  
  54.         };  
  55.         GridView grid = (GridView)findViewById(R.id.grid01);  
  56.         grid.setAdapter(ba);  
  57.     }  
  58. }  
package WangLi.Resources.Values;

import android.app.Activity;
import android.content.res.Resources;
import android.content.res.TypedArray;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.GridView;
import android.widget.TextView;

public class ValuesResTest extends Activity {
    /** Called when the activity is first created. */
	String[] texts;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        texts = getResources().getStringArray(R.array.string_arr);
        //创建一个BaseAdapter对象
        BaseAdapter ba = new BaseAdapter()
        {
        	public int getCount()
        	{
        		return texts.length;
        	}
        	
        	public Object getItem(int position)
        	{
        		return texts[position];
        	}
        	
        	public long getItemId(int position)
        	{
        		return position;
        	}
        	
        	public View getView(int position,View convertView,ViewGroup parent)
        	{
        		TextView text = new TextView(ValuesResTest.this);
        		Resources res = ValuesResTest.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);
        grid.setAdapter(ba);
    }
}


从上面可以看到,定义了字符串,数字,尺寸,数组资源xml文件,在代码中能很容易的使用到它们,在动态生成的R资源类里面可以很轻松的找到它们,系统运行效果如下

 

初学Android,字符串,数字,尺寸,数组资源(十二) .

你可能感兴趣的:(android)