以notepad为例
1、以下是/res/values/strings.xml中定义的字符串值和资源名称
<?xml version="1.0" encoding="utf-8"?>
<!-- Copyright (C) 2007 The Android Open Source Project
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
-->
<resources>
<string name="menu_delete">Delete</string>
<string name="menu_insert">Add note</string>
<string name="menu_revert">Revert</string>
<string name="menu_discard">Discard</string>
<string name="resolve_edit">Edit note</string>
<string name="resolve_title">Edit title</string>
<string name="title_create">Create note</string>
<string name="title_edit">Edit note</string>
<string name="title_notes_list">Note pad</string>
<string name="title_note">Note</string>
<string name="title_edit_title">Note title:</string>
<string name="app_name">Note Pad</string>
<string name="live_folder_name">Notes</string>
以 <string name="title_notes_list">Note pad</string> 为例,title_notes_list为资源名称,Note pad为资源值,只要任何XML文件的结构与上面文件的结构类似,也在/res/values目录下,则它可以做为android的资源XML文件
2 R.java中定义一个内部类,关于字符串资源,其中值 为INT型的资源ID
public static final class string {
public static final int app_name=0x7f04000b;
public static final int button_ok=0x7f04000d;
public static final int error_message=0x7f04000f;
public static final int error_title=0x7f04000e;
public static final int live_folder_name=0x7f04000c;
public static final int menu_delete=0x7f040000;
public static final int menu_discard=0x7f040003;
public static final int menu_insert=0x7f040001;
public static final int menu_revert=0x7f040002;
public static final int resolve_edit=0x7f040004;
public static final int resolve_title=0x7f040005;
public static final int title_create=0x7f040006;
public static final int title_edit=0x7f040007;
public static final int title_edit_title=0x7f04000a;
public static final int title_note=0x7f040009;
public static final int title_notes_list=0x7f040008;
}
3.对某个资源的使用
比如说title_note,可使用R.string.title_note来使用资源ID
4.布局资源
R.java中定义了
public static final class layout {
public static final int note_editor=0x7f030000;
public static final int noteslist_item=0x7f030001;
public static final int title_editor=0x7f030002;
}
其中note_editor为定义在/res/layout/下的note_editor.xml,/res/layout目录下的每个文件 都会根据文件名(不包括扩展名)生成一个唯一常量。对于布局来说,重要的是文件编号
打开note_editor.xml可以看到
以下是文件内容
<?xml version="1.0" encoding="utf-8"?>
<!-- Copyright (C) 2007 The Android Open Source Project
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
-->
<view xmlns:android="http://schemas.android.com/apk/res/android"
class="com.example.android.notepad.NoteEditor$LinedEditText"
android:id="@+id/note"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@android:color/transparent"
android:padding="5dip"
android:scrollbars="vertical"
android:fadingEdge="vertical"
android:gravity="top"
android:textSize="22sp"
android:capitalize="sentences"
/>
1)布局XML文件定义的视图,可在java代码中通过R.java中生成的资源ID访问:
TextView tv=(TextView)this.findViewById(R.id.note_editor)
2) android:id="@+id/note"
"@+id/note"的表示
+表示如果ID note不存在,就创建它