1. 相關文件夾介紹
目錄Directory |
資源類型Resource Types |
res/anim/ |
XML文件,它們被編譯進逐幀動畫(frame by frame animation)或補間動畫(tweened animation)對象 |
res/drawable/ |
.png、.9.png、.jpg文件,它們被編譯進以下的Drawable資源子類型中: 要獲得這種類型的一個資源,可以使用Resource.getDrawable(id) 為了獲取資源類型,使用mContext.getResources().getDrawable(R.drawable.imageId) 注意:放在這里的圖像資源可能會被aapt工具自動地進行無損壓縮優化。比如,一個真彩色但並不需要256色的PNG可能會被轉換為一個帶調色板的8位PNG。這使得同等質量的圖片占用更少的資源。所以我們得意識到這些放在該目錄下的二進制圖像在生成時可能會發生變化。如果你想讀取一個圖像位流並轉換成一個位圖(bitmap),請把圖像文件放在res/raw/目錄下,這樣可以避免被自動優化。 |
res/layout/ |
被編譯為屏幕布局(或屏幕的一部分)的XML文件。參見布局聲明(Declaring Layout) |
res/values/ |
可以被編譯成很多種類型的資源的XML文件。 注意: 不像其他的res/文件夾,它可以保存任意數量的文件,這些文件保存了要創建資源的描述,而不是資源本身。XML元素類型控制這些資源應該放在R類的什么地方。 盡管這個文件夾里的文件可以任意命名,不過下面使一些比較典型的文件(文件命名的慣例是將元素類型包含在該名稱之中): array.xml 定義數組 colors.xml 定義color drawable和顏色的字符串值(color string values)。使用Resource.getDrawable()和Resources.getColor()分別獲得這些資源。 dimens.xml定義尺寸值(dimension value)。使用Resources.getDimension()獲得這些資源。 strings.xml定義字符串(string)值。使用Resources.getString()或者Resources.getText()獲取這些資源。getText()會保留在UI字符串上應用的豐富的文本樣式。 styles.xml 定義樣式(style)對象。 |
res/xml/ |
任意的XML文件,在運行時可以通過調用Resources.getXML()讀取。 |
res/raw/ |
直接復制到設備中的任意文件。它們無需編譯,添加到你的應用程序編譯產生的壓縮文件中。要使用這些資源,可以調用Resources.openRawResource(),參數是資源的ID,即R.raw.somefilename。 |
2.自動生成的R class
3. 在代碼中使用資源
- // Load a background for the current screen from a drawable resource.
- this.getWindow().setBackgroundDrawableResource(R.drawable.my_background_image);
- // WRONG Sending a string resource reference into a
- // method that expects a string.
- this.getWindow().setTitle(R.string.main_title);
- // RIGHT Need to get the title from the Resources wrapper.
- this.getWindow().setTitle(Resources.getText(R.string.main_title));
- // Load a custom layout for the current screen.
- setContentView(R.layout.main_screen);
- // Set a slide in animation for a ViewFlipper object.
- mFlipper.setInAnimation(AnimationUtils.loadAnimation(this,
- R.anim.hyperspace_in));
- // Set the text on a TextView object.
- TextView msgTextView = (TextView)findViewByID(R.id.msg);
- msgTextView.setText(R.string.hello_message);
- //在屏幕上顯示標准應用程序的圖標
- public class MyActivity extends Activity {
- public void onStart() {
- requestScreenFeatures(FEATURE_BADGE_IMAGE);
- super.onStart();
- setBadgeResource(android.R.drawable.sym_def_app_icon);
- }
- }
- //應用系統定義的標准"綠色背景"視覺處理
- public class MyActivity extends Activity
- public void onStart() {
- super.onStart();
- setTheme(android.R.style.Theme_Black);
- }
- }
4. xml文件內引用資源
- <?xml version="1.0" encoding="utf-8"?>
- <resources>
- <string name="hello">Hello World, HelloDemo!</string>
- </resources>
5. 替換資源(為了可替換的資源和配置)
6. Color Value
- <color name="color_name">#color_value</color>
- <?xml version="1.0" encoding="utf-8"?>
- <resources>
- <color name="opaque_red">#f00</color>
- <color name="translucent_red">#80ff0000</color>
- </resources>
7.Color Drawables
- <drawable name="color_name">color_value</drawable>
- <?xml version="1.0" encoding="utf-8"?>
- <resources>
- <drawable name="opaque_red">#f00</drawable>
- <drawable name="translucent_red">#80ff0000</drawable>
- </resources>
8. 圖片
9. dimension
- <dimen name="dimen_name">dimen_value單位</dimen>
Java: float dimen = Resources.getDimen(R.dimen.some_name)
- <?xml version="1.0" encoding="utf-8"?>
- <resources>
- <dimen name="one_pixel">1px</dimen>
- <dimen name="double_density">2dp</dimen>
- <dimen name="sixteen_sp">16sp</dimen>
- </resources>
10. string
- //不使用轉義符則需要用雙引號包住整個string
- <string name="good_example">"This'll work"</string>
- //使用轉義符
- <string name="good_example_2">This\'ll also work</string>
- //錯誤
- <string name="bad_example">This won't work!</string>
- //錯誤 不可使用html轉義字符
- <string name="bad_example_2">XML encodings won't work either!</string>
- <?xml version="1.0" encoding="utf-8"?>
- <resources>
- <string name="simple_welcome_message">Welcome!</string>
- <string name="styled_welcome_message">We are <b><i>so</i></b> glad to see you.</string>
- </resources>
- <TextView
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"
- android:textAlign="center"
- android:text="@string/simple_welcome_message"/>
- // Assign a styled string resource to a TextView on the current screen.
- CharSequence str = getString(R.string.styled_welcome_message);
- TextView tv = (TextView)findViewByID(R.id.text);
- tv.setText(str);
- <?xml version="1.0" encoding="utf-8"?>
- <resources>
- <string name="search_results_resultsTextFormat">%1$d results for <b>&quot;%2$s&quot;</b></string>
- </resources>
- //title是我們想賦值給%2$s的字符串
- String escapedTitle = TextUtil.htmlEncode(title);
- String resultsTextFormat = getContext().getResources().getString(R.string.search_results_resultsTextFormat);
- String resultsText = String.format(resultsTextFormat, count, escapedTitle);
- CharSequence styledResults = Html.fromHtml(resultsText);
11. assets文件夾資源的訪問
需要注意的是,來自Resources和Assets 中的文件只可以讀取而不能進行寫的操作
以下為從Raw文件中讀取:
代碼
public String getFromRaw(){
try {
InputStreamReader inputReader = new InputStreamReader( getResources().openRawResource(R.raw.test1));
BufferedReader bufReader = new BufferedReader(inputReader);
String line="";
String Result="";
while((line = bufReader.readLine()) != null)
Result += line;
return Result;
} catch (Exception e) {
e.printStackTrace();
}
}
以下為直接從assets讀取
代碼
public String getFromAssets(String fileName){
try {
InputStreamReader inputReader = new InputStreamReader( getResources().getAssets().open(fileName) );
BufferedReader bufReader = new BufferedReader(inputReader);
String line="";
String Result="";
while((line = bufReader.readLine()) != null)
Result += line;
return Result;
} catch (Exception e) {
e.printStackTrace();
}
}
當然如果你要得到內存流的話也可以直接返回內存流!