android獲取string.xml的值

encienqi發表於2012-07-30

2012-02-28 09:22:28

為什麼需要把應用中出現的文字單獨存放在string.xml檔案中呢?

一:是為了國際化,當需要國際化時,只需要再提供一個string.xml檔案,把裡面的漢子資訊都修改為對應的語言(如,English),再執行程式時,android作業系統會根據使用者手機的語言環境和國家來自動選擇相應的string.xml檔案,這時手機介面就會顯示出英文。這樣做國際化非常的方便。


二:為了減少應用的體積,降低資料的冗餘。假設在應用中要使用"我們一直在努力"這段文字1000次,如果在每次使用時直接寫上這幾個字,這樣下來程式中將有70000個字,這70000個字佔136KB的空間。而由於手機的資源有限,其CPU的處理能力及記憶體是非常有限的,   136KB 對手機記憶體來說是個不小的空間,我們在做手機應用是一定要記住“能省記憶體就省記憶體”。而如果將這幾個字定義在string.xml中,在每次使用到的地方通過Resources類來引用該文字,只佔用到了14B,因此對降低應用體積效果是非常有效地.當然我們可能在開發時可能並不會用到這麼多的文字資訊,但是,作為手機應用開發人員,我們一定要養成良好的程式設計習慣。


獲取string.xml檔案裡面的值有幾個不同的地方。

1.在AndroidManifest.xml與layout等xml檔案裡:

android:text="@string/resource_name" 

  

2.在activity裡:

方法一:this.getString(R.string.resource_name);  

方法二:getResources().getString(R.string.resource_name); 

 

3.在其他java檔案(必須有Context或application)

方法一: context.getString(R.string.resource_name); 

方法二: application.getString(R.string.resource_name);  



android中string.xml檔案的使用

1.在程式中獲取string.xml中字串和數值

<?xml version="1.0" encoding="utf-8"?>

<resources>

    <string name="hello">Hello World, MainActivity!</string>

    <string name="app_name">TestExample01</string>

</resources>

在Activity中使用:

String appName=(String) this.getResources().getText(R.string.app_name);

或者:

String appName=(String) this.getResources().getString(R.string.app_name);


2.定義string陣列(arrays.xml)

<?xml version="1.0" encoding="utf-8"?>

<resources>

    <string-array name="sports">

  <item>足球</item>

  <item>籃球</item>

  <item>太極</item>

  <item>冰球</item>

    </string-array>

</resources>

----getResources().getStringArray(R.string.sports);



3.定義顏色(colors.xml)

<?xml version="1.0" encoding="utf-8"?>

<resources>

    <color name="black">#FFFFFF</color>

</resources>

---getResources().getDrawable(R.string.black);

---getResources().getColor(R.string.black);



4.定義尺寸(dimens.xml)

<?xml version="1.0" encoding="utf-8"?>

<resources>

   <dimen name="height">80dip</dimen>

</resources>

---getResource().getDimension(R.string.height);



5.定義樣式(styles.xml)

<?xml version="1.0" encoding="utf-8"?>

<resources>

    <style name="sharpText">

  <item name="android:textSize">18sp</item>

  <item name="android:textColor">#000000</item>

    </style>

</resources>

android獲取string.xml的值


assets資料夾資源的訪問


assets資料夾裡面的檔案都是保持原始的檔案格式,需要用AssetManager以位元組流的形式讀取檔案。

1. 先在Activity裡面呼叫getAssets() 來獲取AssetManager引用。

2. 再用AssetManager的open(String fileName, int accessMode) 方法則指定讀取的檔案以及訪問模式就能得到輸入流InputStream。 

3. 然後就是用已經open file 的inputStream讀取檔案,讀取完成後記得inputStream.close() 。

4.呼叫AssetManager.close() 關閉AssetManager。

需要注意的是,來自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(); 

            }

    } 

當然如果你要得到記憶體流的話也可以直接返回記憶體流!

相關文章