android獲取string.xml的值

爲何須要把應用中出現的文字單獨存放在string.xml文件中呢? html

一:是爲了國際化,當須要國際化時,只須要再提供一個string.xml文件,把裏面的漢子信息都修改成對應的語言(如,English),再運行程序時,android操做系統會根據用戶手機的語言環境和國家來自動選擇相應的string.xml文件,這時手機界面就會顯示出英文。這樣作國際化很是的方便。 java


二:爲了減小應用的體積,下降數據的冗餘。假設在應用中要使用"咱們一直在努力"這段文字1000次,若是在每次使用時直接寫上這幾個字,這樣下來程序中 將有70000個字,這70000個字佔136KB的空間。而因爲手機的資源有限,其CPU的處理能力及內存是很是有限的,  136KB 對手機內存來講是個不小的空間,咱們在作手機應用是必定要記住「能省內存就省內存」。而若是將這幾個字定義在string.xml中,在每次使用到的地方經過Resources類來引用該文字,只佔用到了14B,所以對下降應用體積效果是很是有效地.固然咱們可能在開發時可能並不會用到這麼多的文字信息,可是,做爲手機應用開發人員,咱們必定要養成良好的編程習慣。 android


獲取string.xml文件裏面的值有幾個不一樣的地方。 編程

1.在AndroidManifest.xml與layout等xml文件裏: 數組

android:text="@string/resource_name"  app


  


2.在activity裏: this

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

方法二:getResources().getString(R.string.resource_name);  操作系統

 

3.在其餘java文件(必須有Context或pplication) xml

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

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



androidstring.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(); 

      }

  } 

固然若是你要獲得內存流的話也能夠直接返回內存流!
相關文章
相關標籤/搜索