Android的res目录

1. 目录名称

android的res目录是有规定的,分别是:

drawable

Bitmap files (.png, .9.png, .jpg, .gif) or XML files that are compiled into the following drawable resource subtypes:

Bitmap files

Nine-Patches (re-sizable bitmaps)

State lists

Shapes

Animation drawables

Other drawables

layout

XML files that define a user interface layout.

values

 

 XML files that contain simple values, such as strings, integers, and colors.

anim

XML files that define tween animations.

xml

Arbitrary XML files that can be read at runtime by calling Resources.getXML(). Various XML configuration files must be saved here, such as a searchable configuration.
raw

Arbitrary files to save in their raw form. To open these resources with a raw InputStream, call Resources.openRawResource() with the resource ID, which is R.raw.filename.
color

XML files that define a state list of colors.

 

menu

XML files that define application menus, such as an Options Menu, Context Menu, or Sub Menu.

如果不是以上目录会报"invalid resource directory name"的错误。

 

2. 目录下的文件名

在1中所述的res目录下的文件的文件名是有命名要求:must contain only [a-z0-9_.]

也就是说文件名只能包含小写字母、数字和下划线,

否则就会报错"invalid file name: must contain only [a-z0-9_.]"。

 

3. 目录资源的访问

res目录资源通过Resources的相应方法来访问。

相关的信息可以这样获取:

 

String packageName = context.getPackageName(); //包名 
Field[] fields = R.drawable.class.getDeclaredFields();
for (Field field : fields) { 
         String name = field.getName(); //资源文件名称 
} 
this.getResources().getIdentifier(name, "drawable", packageName); 

  

 


 

你可能感兴趣的:(xml,android)