res/value目录XML文件<resource>子标签解析 :
-- string标签 : 代表一个字符串;3、分类资源文件 :
如果将所有的资源放到一个XML文件中, 会增加维护难度, 这里将不通类型的资源放到不同的XML文件下;
-- styles.xml : 存放样式资源;
二、Android中资源的使用
(1) Java代码访问清单资源
在Java代码中通过R类获取资源语法 :
[packageName.] R . resourceType . resourceName
(2) Java代码访问原生资源
Resource类 : Android资源访问控制类, 该类提供了大量方法获取实际资源, Resource通过 Context.getResource()方法获得;
-- 获取清单资源 : resource.getString(id), 根据id获取实际资源;
-- 获取原生资源 : resource.getassets(), 获取AssetManager对象;
//获取Resource资源, 这个方法在Activity中执行 Resources resources = getResources(); //获取字符串资源 String hello = resources.getString(R.string.hello_world); //获取图片资源 Drawable luncher = resources.getDrawable(R.drawable.ic_launcher);
@ [packageName : ] resourceType / resourceName
三、字符串 颜色 尺寸 数组资源的使用情况
(1) 几种资源的目录引用名称
字符串资源 :
-- 默认目录 : /res/values/strings.xml ;
-- 引用方式 : R.string.xxx ;
颜色资源 :
-- 默认目录 : /res/values/colors.xml ;
-- 引用方式 : R.color.xxx ;
尺寸资源 :
-- 默认目录 : /res/values/dimens.xml ;
-- 引用方式 : R.dimens.xxx ;
PS
颜色定义方式:
三原色 : 白光 可以分解为 红, 绿, 蓝 三种颜色的光, 红绿蓝都是最大值的时候就是白色, 三种值相等, 但不是最大值是灰色, 如果其中一种或两种值比较大, 就会产生各种颜色的彩色;
颜色表示 : 颜色通过 红(red) 绿(green) 蓝(blue) 三种颜色, 以及 透明度(alpha) 来表示的;
-- 颜色开头 : 颜色值总是以 # 开头;
-- 无透明度 : 如果没有 alpha 值, 默认完全不透明;
颜色定义形式 :
-- #RGB : 红 绿 蓝 三原色值, 每个值分16个等级, 最小为0, 最大为f;
-- #ARGB : 透明度 红 绿 蓝 值, 每个值分16个等级, 最小为0, 最大为f;
-- #RRGGBB : 红 绿 蓝 三原色值, 每个值分 256个等级, 最小为0, 最大为ff;
-- #AARRGGBB : 透明度 红 绿 蓝 值, 每个值分 256个等级, 最小为0, 最大为ff;
(2)字符串 颜色 尺寸 XML文件定义
1) 字符串资源文件
字符串资源文件信息 :
-- 资源位置 : /res/values 目录下;
-- 根元素 : <resource>是根元素;
-- 子元素 : <string />;
-- name属性 : 指定变量名称;
-- 标签文本 : 标签文本就是字符串信息;
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="app_name">ResourceTest</string>
<string name="action_settings">Settings</string>
<string name="hello_world">Hello world!</string>
</resources> <?xml version="1.0" encoding="utf-8"?>
<resources>
<color name = "red">#FF4000</color>
<color name = "black">#120A2A</color>
<color name = "green">#00FF00</color>
<color name = "yellow">#FFFF00</color>
</resources> <resources>
<!-- Default screen margins, per the Android Design guidelines. -->
<dimen name="activity_horizontal_margin">16dp</dimen>
<dimen name="activity_vertical_margin">16dp</dimen>
</resources> 4)数组资源
资源数组文件 : 通常将数组定义在 /res/values/arrays.xml文件中;
-- 根标签 : <resources> ;
-- 子标签 : <array>, <string-array>, <integer-array>;
资源数组类型 : 数组的资源的跟标签都是 <resources>, 不同类型的数组的子元素不同;
-- 普通类型数组 : 使用<array>作为子元素标签;
-- 字符串数组 : 使用<string-array>作为子元素标签;
-- 整数数组 : 使用<integer-array>作为子元素标签;
XML文件中调用数组资源 : @ [packageName :] array/arrayName ;
Java文件中调用数组资源 : [packageName . ]R.array.arrayName ;
-- 获取实际普通数组 : TypeArray obtainTypedArray(int id), 根据普通数组资源名称获取实际普通数组, TypeArray类提供了getXxx(int index)方法获取指定索引的元素;
-- 获取字符串数组 : String[] getStringArray(int id), 根据字符串数组资源名称获取字符串数组;
-- 获取整数数组 : int[] getIntArray(int id), 根据整数数组资源名称获取实际的整数数组;
示例 :
colors.xml
<?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<?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
<?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>
arrays.xml
<?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> <?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>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);
}
} 最后还要补充的就是还有布尔和整数资源了。
整数资源:
/res/values/integer.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
<integer name = "size_1">10</integer>
<integer name = "size_2">20</integer>
</resources> java代码import android.app.Activity;
import android.content.res.Resources;
import android.os.Bundle;
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//获取Resource资源, 这个方法在Activity中执行
Resources resources = getResources();
int size_1 = resources.getInteger(R.integer.size_1);
System.out.println(size_1);
}
} /res/values/bool.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
<bool name = "is_true">true</bool>
<bool name = "is_false">false</bool>
</resources> import android.app.Activity;
import android.content.res.Resources;
import android.os.Bundle;
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//获取Resource资源, 这个方法在Activity中执行
Resources resources = getResources();
boolean is_true = resources.getBoolean(R.bool.is_true);
System.out.println(is_true);
}
}
Android应用资源的使用方法(数组、颜色、尺寸、字符串、布尔、整数),布布扣,bubuko.com
Android应用资源的使用方法(数组、颜色、尺寸、字符串、布尔、整数)
原文:http://blog.csdn.net/u011067360/article/details/24848887