资源文件

来源:互联网 发布:王心凌怎么消失了知乎 编辑:程序博客网 时间:2024/05/29 08:30
1.res目录下的资源文件
assets目录下保存着无法通过R清单类访问的原生资源;
以下都可以通过R清单访问:
/res/animator:属性动画XML;
/res/anim:补间动画XML;
/res/color:不同状态下颜色列表的XML;
/res/drawable:各种图;
/res/layout:用户界面布局文件;
/res/menu:菜单资源;
/res/raw:任意类型原生资源
/res/values:各种简单的XML值,都是<resources../>
                      arrays.xml:定义数组资源;
                      colors.xml:定义颜色资源;              对应R类中的内部类名称为R.colors
                      dimens.xml:定义尺寸值资源;         对应R类中的内部类名称为R.dimen
                      strings.xml:定义字符串资源;          对应R类中的内部类名称为R.string
                      style.xml:定义样式资源;
               也可bools.xml:定义布尔型量;
                      integer.xml:定义整型;

2.Java代码中访问实际资源

Resources类提供了大量方法根据资源清单ID获取实际资源,主要是两种方法,Context调用getResources()获取:
getXxx(int id):根据资源清单ID来获取实际资源;
       如getDimension:获得长度单位。
getAssests():获取访问/assets/目录下资源的AssetManager对象
  1. //直接调用Activity的getResource()方法来获取Resource对象
  2. Resource res = getResource();
  3. //获取字符串资源
  4. String mainTitle = res.getText(R.string.main_title);
  5. //获取Drawable资源
  6. Drawable logo = res.getDrawable(R.drawable.logo);
  7. //获取数组资源
  8. int[] arr = res.getIntArray(R.array.books);

3.定义和使用values资源

定义字符串
  1. <?xml version="1.0" encoding="utf-8"?>
  2. <resources>
  3. <string name="app_name">字符串、数字、尺寸资源</string>
  4. </resources>
使用@string/app_name

定义颜色
  1. <?xml version="1.0" encoding="utf-8"?>
  2. <resources>
  3. <color name="颜色名称">#F00</string>
  4. </resources>
使用@color/name

定义尺寸
  1. <?xml version="1.0" encoding="utf-8"?>
  2. <resources>
  3. <dimen name="尺寸名称">9dp</string>
  4. <dimen name="cell_width">60dp</string>
  5. </resources>
使用@dimen/name

定义数组资源
有三种子元素
<array.../>:定义普通类型的数组;
<string-array.../>:定义字符串数组;
<integer-array/>:整形数组;
  1. <?xml version="1.0" encoding="utf-8"?>
  2. <resources>
  3. <!-- 定义一个Drawable数组 -->
  4. <array name="plain_arr">
  5. <item>@color/c1</item>
  6. <item>@color/c2</item>
  7. <item>@color/c3</item>
  8. </array>
  9. <!-- 定义字符串数组 -->
  10. <string-array name="string_arr">
  11. <item>@string/c1</item>
  12. <item>@string/c2</item>
  13. <item>@string/c3</item>
  14. </string-array>
  15. <!-- 定义字符串数组 -->
  16. <string-array name="books">
  17. <item>字符串1</item>
  18. <item>字符串2</item>
  19. <item>字符串3</item>
  20. </string-array>
  21. </resources>
Java程序中访问使用到的方法:
String[] getStringArray(int id):根据资源文件中字符串数组资源的名称来获取实际的字符串数组;
  1. String[] texts = getResources().getStringArray(R.array.string_arr);
int[] getIntArray(int id):根据资源文件中整形数组资源的名称来获取实际的整形数组;
TypedArray obtainTypedArray(int id):根据资源文件中普通数组资源的名称来获取实际的普通数组,TypedArray是一个通用类型数组,该类提供了getXxx(int index)
                                                           方法来获取指定索引处元素值;
  1. TypedArray icons = res.obtainTypedArray(R.array.plain_arr);
  2. //使用颜色资源来设置文本框的背景色
  3. text.setBackgroundDrawable(icons.getDrawable(position));
Style和Theme资源
根元素也是<resource../>,包含多个<style../>子元素可定义样式,
<style../>有两个属性:name和parent。
样例:
  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <resources>
  3. <!--定义了一个样式,指定字体大小、字体颜色 --->
  4. <style name = "style1">
  5. <item name="android:textSize">20sp</item>
  6. <item name="android:textColor">#00d</item>
  7. </style>
  8. <style name = "style2" parent="@style/style1">
  9. <item name="android:background">#ee6</item>
  10. <item name="android:padding">8dp</item>
  11. <!--覆盖父类式中指定的属性-->
  12. <item name = "android:textColor">#000</item>
  13. </style>
  14. </resources>
主题也使用<style.../>来定义主题
不同在:主题不能做为单个的View组件,而是对整个应用中所有Activity起作用。主题应该是改变窗口外观的样式。
样例:
  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <resources>
  3. <style name = "myTheme">
  4. <item name="android:WindowNoTitle">true</item>
  5. <item name="android:WindowFullscreen">true</item>
  6. <item name="android:WindowFrame">@drawable/window_border</item>
  7. <item name="android:WindowBackground">@drawable/star</item>
  8. </style>
  9. </resources>
使用时可以在Java中的Activity使用主题,setThem(R.style.myTheme
在Manifest文件中application和Activity都可使用,android:theme="@style/myTheme"
主题支持继承 
<style name = "myTheme" parent"@android:style/Theme.Dialog">

3.定义和使用Drawable资源

   StateListDrawable资源
   StateListDrawable对象所显示的Drawable对象会随目标组件状态的改变而自动切换。对应的XML元素为<selector.../>。包含<item.../>元素,元素属性有:
   android:color
   android;drawable
   android:state_xxx
                  state_active:是否处于激活状态;
                  state_checkable:是否处于可勾选状态;
                  state_enabled:是否可用状态;
                  state_first:是否处于开始状态;
                  state_focused:是否处于已获得焦点状态;
                  state_last:是否处于结束状态;
                  state_middle:是否处于中间状态;
                  state_pressed:是否被按下状态;
                  state_selected:是否被选中状态;
                  state_window_focused:是否窗口已获得焦点状态;
  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <selector xmlns:android="http://schemas.android.com/apk/res/android">
  3. <!-- 指定获得焦点时的颜色 -->
  4. <item android:state_focused="true"
  5. android:color="#f44"/>
  6. <!-- 指定失去焦点时的颜色 -->
  7. <item android:state_focused="false"
  8. android:color="#eee"/>
  9. </selector>

   LayoutDrawable资源
   根元素为<Layer-list.../>,系统将会按这些Drawable对象的数组顺序来绘制它们,索引最大的Drawable对象将会被绘制在最上面。
   属性:
   android:drawable: 指定做为LayoutDrawable元素之一的Drawable对象;
   android:id   为该Drawable对象指定一个标识;
   android:buttom|top|left|button:指定一个长度值,用于指定将该Drawable
   
   ShaprDrawable资源
   根元素<shape.../>用于定义一个基本的几何图形,用来作为文本框等background属性,可做出各种外观的文本框,可以指定:
   android:shape=rectangle|oval|line|ring指定哪种类型的几何图形。
   完整语法格式:
  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <shape xmlns:android="http://schemas.android.com/apk/res/android"
  3. android:shape=["rectangle"|"oval"|"line"|"ring"]>
  4. <!-- 定义几何图形四个角的弧度-->
  5. <corners
  6. android:radius="integer"
  7. android:topLeftRadius="integer"
  8. android:topRightRadius="integer"
  9. android:bottomLedtRadius="integer"
  10. android:bottomRightRadius="integer"/>

  11. <!-- 定义使用的渐变颜色填充-->
  12. <gradient
  13. android:angle="integer"
  14. android:centerX="integer"
  15. android:centerY="integer"
  16. android:centerColor="integer"
  17. android:endColor="color"
  18. android:grandientRadius="integer"
  19. android:startColor="integer"
  20. android:type=["linear"|"radial"|"sweep"]
  21. android:usesLevel= ["true"|"false"] />
  22. android:topRightRadius="integer"

  23. <!-- 定义几何形状的内边距-->
  24. <padding
  25. android:left="integer"
  26. android:top="integer"
  27. android:right="integer"
  28. android:bottom="integer"/>  

  29. <!-- 定义几何形状的大小-->
  30. <size
  31. android:width="integer"
  32. android:top="integer"
  33. android:right="integer"
  34. android:bottom="integer" />  

  35. <!-- 定义使用单种颜色填充-->
  36. <solid
  37. android:color="color" />

  38. <!-- 定义为几何形状绘制边框-->
  39. <stroke
  40. android:width="integer"
  41. android:color="color"
  42. android:dashWidth="integer"
  43. android:dashGap="integer" />
  44. </shape>
   ClipDrawable资源
   根元素<clip../>,代表从其他位图上截取一个"图片片段"。



                     


来自为知笔记(Wiz)


0 0
原创粉丝点击