Android - 资源的定义和引用

Android中的字符串、颜色值、数组等资源多是定义在xml文件中,ADT会为每个资源生成一个ID并存放在R.java中。代码中,我们就使用R.java的这些资源的ID。

定义字符串(res/values/strings.xml)

  1. <string name="hello">Hello</string>  

在代码中取得其引用:

  1. getResources().getString(R.string.hello);  

在xml文件中取得其引用:

  1. android:text="@string/hello"  

定义颜色(res/values/color.xml):

  1. <resources>  
  2.     <color name="red">#f00</color>  
  3.     <color name="blue">#0000ff</color>  
  4.     <color name="green">#f0f0</color>  
  5.     <color name="main_back_ground_color">#ffffff00</color>  
  6. </resources>  

在代码中取得其引用:

  1. activity.getResources.getColor(R.color.red);  

在xml文件中取得其引用:

  1. android:textColor="@color/red"  

定义数组(res/values/color.xml):

  1. <array name="integer_array">  
  2.     <item>1</item>  
  3.     <item>2</item>  
  4.     <item>3</item>  
  5. </array>  

在代码中取得其引用:

  1. int[] arr = getResources().getIntArray(R.array.integer_array);  

定义字符数组(res/values/array.xml):

  1. <string-array name="drawings">  
  2.     <item>随笔</item>  
  3.     <item>直线</item>  
  4.     <item>矩形</item>  
  5.     <item>椭圆</item>  
  6.     <item>圆形</item>  
  7.     <item>圆点</item>  
  8.     <item>文字</item>  
  9. </string-array>  

在代码中取得其引用:

  1. String[] arr = getResources().getStringArray(R.array.drawings);  

在xml文件中取得其引用:

  1. android:entryValues="@array/drawings"  

定义style(res/values/*.xml)

  1. <mce:style name="nomarl_text"><!--  
  2.     <item name="android:textSize">8sp</item>  
  3.     <item name="android:textColor">#111</item>  
  4. --></mce:style><style name="nomarl_text" mce_bogus="1">    <item name="android:textSize">8sp</item>  
  5.     <item name="android:textColor">#111</item></style>  

在xml文件中取得其引用:

  1. android:theme="@style/nomarl_text"  

定义View(res/layout/main.xml)

  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     android:orientation="vertical"  
  4.     android:layout_width="fill_parent"  
  5.     android:layout_height="fill_parent"  
  6.     >  
  7. <TextView    
  8.     android:layout_width="fill_parent"  
  9.     android:layout_height="wrap_content"  
  10.     android:text="@string/hello"  
  11.     />  
  12. </LinearLayout>  

在代码中取得其引用:

  1. setContentView(R.layout.main);  

定义menu(res/menu/menu_main.xml)

  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <menu xmlns:android="http://schemas.android.com/apk/res/android">  
  3.     <item  
  4.         android:title="设置"  
  5.         android:id="@+id/setting_menu_item"  
  6.         android:icon="@android:drawable/ic_menu_preferences">  
  7.     </item>  
  8.       
  9.     <item  
  10.         android:title="保存"  
  11.         android:id="@+id/save_menu_item"  
  12.         android:icon="@android:drawable/ic_menu_save">  
  13.     </item>  
  14.       
  15.     <item  
  16.         android:id="@+id/what_to_draw"  
  17.         android:title="图形">  
  18.     </item>  
  19. </menu>  

在代码中取得其引用:

  1. MenuInflater inflater = getMenuInflater();  
  2. inflater.inflate(R.menu.menu_main, menu);  

定义preferences(res/xml/settings.xml)

  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android">  
  3.     <PreferenceCategory  
  4.         android:title="@string/pen_title"  
  5.         android:key="@string/pen_key"  
  6.         android:summary="@string/pen_summary">  
  7.         <Preference  
  8.             android:summary="@string/pen_width_summary"  
  9.             android:title="@string/pen_width_title"  
  10.             android:key="@string/pen_width_key">  
  11.         </Preference>  
  12.              
  13.         <Preference  
  14.             android:summary="@string/pen_color_summary"  
  15.             android:title="@string/pen_color_title"  
  16.             android:key="@string/pen_color_key">  
  17.         </Preference>  
  18.         <CheckBoxPreference  
  19.             android:summaryOn="@string/pen_style_summary_on"  
  20.             android:summaryOff="@string/pen_style_summary_off"      
  21.             android:title="@string/pen_style_title"  
  22.             android:key="@string/pen_style_key"  
  23.             android:defaultValue="@string/checkbox_default_false">  
  24.         </CheckBoxPreference>  
  25.         <CheckBoxPreference  
  26.             android:summaryOn="@string/pen_antialias_summay_on"  
  27.             android:summaryOff="@string/pen_antialias_summay_off"  
  28.             android:title="@string/pen_antialias_title"  
  29.             android:key="@string/pen_antialias_key"  
  30.             android:defaultValue="@string/checkbox_default_true">  
  31.         </CheckBoxPreference>  
  32.          
  33.     </PreferenceCategory>  
  34.     <PreferenceCategory  
  35.         android:title="@string/canvas_title"  
  36.         android:key="@string/canvas_key">  
  37.          
  38.         <Preference  
  39.             android:key="@string/canvas_bgcolor_key"  
  40.             android:title="@string/canvas_bgcolor_title"  
  41.             android:summary="@string/canvas_bgcolor_summary">  
  42.         </Preference>  
  43.          
  44.     </PreferenceCategory>  
  45. </PreferenceScreen>  

在代码中取得其引用:

  1. addPreferencesFromResource(R.xml.settings);  

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值