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

arrays.xml

[html] view plain copy print ?
  1. <?xmlversion="1.0"encoding="utf-8"?>
  2. <resources>
  3. <arrayname="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-arrayname="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-arrayname="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

[html] view plain copy print ?
  1. <?xmlversion="1.0"encoding="utf-8"?>
  2. <resources>
  3. <colorname="c1">#F00</color>
  4. <colorname="c2">#0F0</color>
  5. <colorname="c3">#00F</color>
  6. <colorname="c4">#0FF</color>
  7. <colorname="c5">#F0F</color>
  8. <colorname="c6">#FF0</color>
  9. <colorname="c7">#07F</color>
  10. <colorname="c8">#70F</color>
  11. <colorname="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

[html] view plain copy print ?
  1. <?xmlversion="1.0"encoding="utf-8"?>
  2. <resources>
  3. <dimenname="spacing">8dp</dimen>
  4. <dimenname="cell_width">60dp</dimen>
  5. <dimenname="cell_height">66dp</dimen>
  6. <dimenname="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

[html] view plain copy print ?
  1. <?xmlversion="1.0"encoding="utf-8"?>
  2. <resources>
  3. <stringname="hello">HelloWorld,ValuesResTest!</string>
  4. <stringname="app_name">字符串,数字,尺寸,数组资源</string>
  5. <stringname="c1">F00</string>
  6. <stringname="c2">0F0</string>
  7. <stringname="c3">00F</string>
  8. <stringname="c4">0FF</string>
  9. <stringname="c5">F0F</string>
  10. <stringname="c6">FF0</string>
  11. <stringname="c7">07F</string>
  12. <stringname="c8">70F</string>
  13. <stringname="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

[html] view plain copy print ?
  1. <?xmlversion="1.0"encoding="utf-8"?>
  2. <LinearLayoutxmlns: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. <TextView
  7. android:layout_width="wrap_content"
  8. android:layout_height="wrap_content"
  9. android:text="@string/app_name"
  10. android:gravity="center"
  11. android:textSize="@dimen/title_font_size"
  12. />
  13. <GridView
  14. android:id="@+id/grid01"
  15. android:layout_width="wrap_content"
  16. android:layout_height="wrap_content"
  17. android:horizontalSpacing="@dimen/spacing"
  18. android:verticalSpacing="@dimen/spacing"
  19. android:numColumns="3"
  20. android:gravity="center">
  21. </GridView>
  22. <ListView
  23. android:id="@+id/listView1"
  24. android:layout_width="wrap_content"
  25. android:layout_height="wrap_content"
  26. android:entries="@array/books"
  27. >
  28. </ListView>
  29. </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>


代码

[java] view plain copy print ?
  1. packageWangLi.Resources.Values;
  2. importandroid.app.Activity;
  3. importandroid.content.res.Resources;
  4. importandroid.content.res.TypedArray;
  5. importandroid.os.Bundle;
  6. importandroid.view.View;
  7. importandroid.view.ViewGroup;
  8. importandroid.widget.BaseAdapter;
  9. importandroid.widget.GridView;
  10. importandroid.widget.TextView;
  11. publicclassValuesResTestextendsActivity{
  12. /**Calledwhentheactivityisfirstcreated.*/
  13. String[]texts;
  14. @Override
  15. publicvoidonCreate(BundlesavedInstanceState){
  16. super.onCreate(savedInstanceState);
  17. setContentView(R.layout.main);
  18. texts=getResources().getStringArray(R.array.string_arr);
  19. //创建一个BaseAdapter对象
  20. BaseAdapterba=newBaseAdapter()
  21. {
  22. publicintgetCount()
  23. {
  24. returntexts.length;
  25. }
  26. publicObjectgetItem(intposition)
  27. {
  28. returntexts[position];
  29. }
  30. publiclonggetItemId(intposition)
  31. {
  32. returnposition;
  33. }
  34. publicViewgetView(intposition,ViewconvertView,ViewGroupparent)
  35. {
  36. TextViewtext=newTextView(ValuesResTest.this);
  37. Resourcesres=ValuesResTest.this.getResources();
  38. //使用尺寸资源来设置文本框的高度,宽度
  39. text.setWidth((int)res.getDimension(R.dimen.cell_width));
  40. text.setHeight((int)res.getDimension(R.dimen.cell_height));
  41. //使用字符串资源设置文本框的内容
  42. text.setText(texts[position]);
  43. TypedArrayicons=res.obtainTypedArray(R.array.plain_arr);
  44. //使用颜色资源来设置文本框的背景色
  45. text.setBackgroundDrawable(icons.getDrawable(position));
  46. text.setTextSize(20);
  47. returntext;
  48. }
  49. };
  50. GridViewgrid=(GridView)findViewById(R.id.grid01);
  51. grid.setAdapter(ba);
  52. }
  53. }
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资源类里面可以很轻松的找到它们,系统运行效果如下

更多相关文章

  1. 分支和循环(二)(零基础学习C语言)
  2. 不吹不黑!GitHub 上帮助人们学习编码的 12 个资源,错过血亏...
  3. 一款霸榜 GitHub 的开源 Linux 资源监视器!
  4. Android与Internet(获取资源,多线程断点下载,get/post发送,发送xml)
  5. android textview 显示表情和文字 表情带超链接
  6. Android(安卓)- SimpleDateFormat 详解
  7. lint工具改错
  8. android中创建有自身特色的字符串
  9. android获取屏幕的尺寸,宽度和高度

随机推荐

  1. C语言函数功能的强大和入坑体会
  2. 迪芝伦(Digilent)推出全新开发板PYNQ-Z1,支
  3. RPA逆势火了
  4. 关于Xilinx Zynq UltraScale+ MPSoC的电
  5. 同态加密技术及其在机器学习中的应用详解
  6. 看Mellanox公司如何降低服务器CPU负载!
  7. 赛灵思FPGA助力解决困扰7年的27皇后难题
  8. 借助NOR Flash实现UltraScale FPGA后配置
  9. 阿里1688实时数据工程实践
  10. JavaWeb系统自学,全栈学习路线图,百度网盘