Home > Web Front-end > JS Tutorial > Overview of the use of resource files (non-code parts) in Android_jquery

Overview of the use of resource files (non-code parts) in Android_jquery

WBOY
Release: 2016-05-16 17:46:18
Original
1735 people have browsed it
Android中的资源访问
  Android中的资源是指非代码部分,指外部文件。
  assets中保存的一般是原生的文件,例如MP3文件,Android程序不能直接访问,必须通过AssetManager类以二进制流的形式来读取。
  res中的资源可以通过R资源类直接访问。
  R类是自动生成的,在该类中根据不同的资源类型生成了相应的内部类,该类包含了系统中使用到的所有资源文件的标识。

1.在代码中使用资源文件
  在代码中访问资源文件,是通过R类中定义的资源文件类型和资源文件名称来访问的。
  具体格式为:
  R.资源文件类型.资源文件名称
  另外,除了访问用户自定义的资源文件,还可以访问系统中的资源文件。
  访问系统中的资源文件的格式为:
  android.R. 资源文件类型.资源文件名称

2.在其他资源文件中引用资源文件
  经常会在布局文件中引用图片、颜色资源、字符串资源和尺寸资源。
  在其他资源中引用资源的一般格式是:
  @[包名称:]资源类型/资源名称

使用颜色资源
  颜色值定义的开始时一个#号,后面是Alpha-RGB的格式。
  例如:
  #RGB
  #ARGB
  #RRGGBB
  #AARRGGBB
  引用资源格式:
  Java代码中:R.color.color_name
  XML文件中:@[package:]color/color_name
  使用时在res\values\目录下,定义一个colors.xml文件,里面存放颜色名字和颜色值的键值对。
  如:
复制代码 代码如下:


#f00
#0000ff


  其他资源如字符串、尺寸都是类似的方法。

使用字符串资源
  创建字符串资源文件strings.xml.里面内容也是键值对
  在Java代码中引用字符串资源R.string.string_name
  可如下取得:
  String str = getString(R.string.string_name).toString();
  在xml文件中引用字符串资源:@[package:]string/string_name

使用尺寸资源
  尺寸资源被定义在res\values\dimens.xml文件中。
  Android中支持的尺寸单位:

单位表示

单位名称

说明

dip 

设备独立像素 

不同设备不同的显示效果,dip与屏幕密度有关 

px

像素

屏幕上的真实像素表示,不同设备不同显示屏显示效果相同

in

英尺

基于屏幕的物理尺寸

mm

毫米

基于屏幕的物理尺寸

pt

点(磅)

英尺的1/72

dp

和密度无关的像素

相对屏幕物理密度的抽象单位

sp

和精度无关的像素

和dp类似,与刻度无关的像素,主要处理字体大小


Reference dimension resource:
In Java code: R.dimen.dimen_name
In xml file: @[package:]dimen/dimen_name

Use original XML resources
XML files are defined in the resxml directory of the project and are accessed through the Resources.getXML() method.
The idea of ​​obtaining the original XML file is to obtain the original XML file through getResources().getXml() and obtain the XmlResourceParser object. Use this object to determine whether it is the beginning or end of the document, the beginning or the end of a certain tag, and Access the content of the XML file by traversing the XML file through some methods of obtaining attributes.

Use drawables resources
Drawable resources are some pictures or color resources, mainly used to draw the screen, obtained through the Resources.getDrawable() method.
Drawable resources are divided into three categories: Bitmap File, Color Drawable, and Nine-Patch Image.
The bitmap files supported in Android include png, jpg and gif.
The format for referencing bitmap resources:
In Java code: R.drawable.file_name
In XML file: @[package:]drawable/file_name

Use layout (layout ) Resources
Layout resources are the most commonly used resources in Android. They define the layout of components on the screen in an XML file, similar to HTML pages on the Web.
The layout file is located in reslayout and has any name. Android parses the components in the XML file into visual view components through the LayoutInflater class.
In the Activity, call the Activity.setContentView() method, set the layout file to the Activity interface, and use the findViewById() method to get the components in the layout.
Reference layout file:
In Java code: R.layout.my_layout
In XML file: @[package:]layout/my_layout

Use menu resources
There are two ways to create any view component: one is by declaring it in the layout file; the other is by creating it in code.
Menus in Android are divided into option menus, context menus and submenus, which can be declared and defined in XML files and used in code through the MenuInflater class.
The menu resource file is also an XML file and is placed in the project resmenu directory. Referenced through R.menu.my_menu.
The structure of a typical menu resource file: root element. In the root element, and sub-elements are nested. elements can also be nested with Form submenu.
With limited time and energy, we cannot do detailed example research on each type. We have to integrate it in actual applications and slowly understand it. come on!
Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template