资源文件
1.res目录下的资源文件
assets目录下保存着无法通过R清单类访问的原生资源;
以下都可以通过R清单访问:
/res/animator:属性动画XML;
/res/anim:补间动画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对象
//直接调用Activity的getResource()方法来获取Resource对象Resource res = getResource();//获取字符串资源String mainTitle = res.getText(R.string.main_title);//获取Drawable资源Drawable logo = res.getDrawable(R.drawable.logo);//获取数组资源int[] arr = res.getIntArray(R.array.books);
3.定义和使用values资源
定义字符串
<?xml version="1.0" encoding="utf-8"?><resources><string name="app_name">字符串、数字、尺寸资源</string></resources>
使用@string/app_name
定义颜色
<?xml version="1.0" encoding="utf-8"?><resources><color name="颜色名称">#F00</string></resources>
使用@color/name
定义尺寸
<?xml version="1.0" encoding="utf-8"?><resources><dimen name="尺寸名称">9dp</string><dimen name="cell_width">60dp</string></resources>
使用@dimen/name
定义数组资源
有三种子元素
<array.../>:定义普通类型的数组;
<string-array.../>:定义字符串数组;
<integer-array/>:整形数组;
<?xml version="1.0" encoding="utf-8"?><resources><!-- 定义一个Drawable数组 --><array name="plain_arr"><item>@color/c1</item><item>@color/c2</item><item>@color/c3</item></array><!-- 定义字符串数组 --><string-array name="string_arr"><item>@string/c1</item><item>@string/c2</item><item>@string/c3</item></string-array><!-- 定义字符串数组 --><string-array name="books"><item>字符串1</item><item>字符串2</item><item>字符串3</item></string-array></resources>
Java程序中访问使用到的方法:
String[] getStringArray(int id):根据资源文件中字符串数组资源的名称来获取实际的字符串数组;
String[] texts = getResources().getStringArray(R.array.string_arr);
int[] getIntArray(int id):根据资源文件中整形数组资源的名称来获取实际的整形数组;
TypedArray obtainTypedArray(int id):根据资源文件中普通数组资源的名称来获取实际的普通数组,TypedArray是一个通用类型数组,该类提供了getXxx(int index)
方法来获取指定索引处元素值;
TypedArray icons = res.obtainTypedArray(R.array.plain_arr);//使用颜色资源来设置文本框的背景色text.setBackgroundDrawable(icons.getDrawable(position));
Style和Theme资源
根元素也是<resource../>,包含多个<style../>子元素可定义样式,
<style../>有两个属性:name和parent。
样例:
<?xml version="1.0" encoding="UTF-8"?><resources><!--定义了一个样式,指定字体大小、字体颜色 ---><style name = "style1"><item name="android:textSize">20sp</item><item name="android:textColor">#00d</item></style><style name = "style2" parent="@style/style1"><item name="android:background">#ee6</item><item name="android:padding">8dp</item><!--覆盖父类式中指定的属性--><item name = "android:textColor">#000</item></style></resources>
主题也使用<style.../>来定义主题
不同在:主题不能做为单个的View组件,而是对整个应用中所有Activity起作用。主题应该是改变窗口外观的样式。
样例:
<?xml version="1.0" encoding="UTF-8"?><resources><style name = "myTheme"><item name="android:WindowNoTitle">true</item><item name="android:WindowFullscreen">true</item><item name="android:WindowFrame">@drawable/window_border</item><item name="android:WindowBackground">@drawable/star</item></style></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:是否窗口已获得焦点状态;
<?xml version="1.0" encoding="UTF-8"?><selector xmlns:android="http://schemas.android.com/apk/res/android"><!-- 指定获得焦点时的颜色 --><item android:state_focused="true"android:color="#f44"/><!-- 指定失去焦点时的颜色 --><item android:state_focused="false"android:color="#eee"/></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指定哪种类型的几何图形。
完整语法格式:
<?xml version="1.0" encoding="UTF-8"?><shape xmlns:android="http://schemas.android.com/apk/res/android"android:shape=["rectangle"|"oval"|"line"|"ring"]><!-- 定义几何图形四个角的弧度--><cornersandroid:radius="integer"android:topLeftRadius="integer"android:topRightRadius="integer"android:bottomLedtRadius="integer"android:bottomRightRadius="integer"/><!-- 定义使用的渐变颜色填充--><gradientandroid:angle="integer"android:centerX="integer"android:centerY="integer"- android:centerColor="integer"
- android:endColor="color"
- android:grandientRadius="integer"
- android:startColor="integer"
- android:type=["linear"|"radial"|"sweep"]
- android:usesLevel= ["true"|"false"] />
android:topRightRadius="integer"<!-- 定义几何形状的内边距--><paddingandroid:left="integer"android:top="integer"android:right="integer"android:bottom="integer"/>- <!-- 定义几何形状的大小-->
<sizeandroid:width="integer"android:top="integer"android:right="integer"android:bottom="integer"/>- <!-- 定义使用单种颜色填充-->
- <solid
- android:color="color" />
- <!-- 定义为几何形状绘制边框-->
- <stroke
- android:width="integer"
- android:color="color"
- android:dashWidth="integer"
- android:dashGap="integer" />
</shape>
ClipDrawable资源
根元素<clip../>,代表从其他位图上截取一个"图片片段"。
勿忘初衷

浙公网安备 33010602011771号