小男孩‘自慰网亚洲一区二区,亚洲一级在线播放毛片,亚洲中文字幕av每天更新,黄aⅴ永久免费无码,91成人午夜在线精品,色网站免费在线观看,亚洲欧洲wwwww在线观看

分享

android獲取string.xml的值

 aaie_ 2016-01-14


為什么需要把應(yīng)用中出現(xiàn)的文字單獨(dú)存放在string.xml文件中呢?

一:是為了國(guó)際化,當(dāng)需要國(guó)際化時(shí),只需要再提供一個(gè)string.xml文件,把里面的漢子信息都修改為對(duì)應(yīng)的語(yǔ)言(如,English),再運(yùn)行程序時(shí),android操作系統(tǒng)會(huì)根據(jù)用戶手機(jī)的語(yǔ)言環(huán)境和國(guó)家來(lái)自動(dòng)選擇相應(yīng)的string.xml文件,這時(shí)手機(jī)界面就會(huì)顯示出英文。這樣做國(guó)際化非常的方便。


二:為了減少應(yīng)用的體積,降低數(shù)據(jù)的冗余。假設(shè)在應(yīng)用中要使用"我們一直在努力"這段文字1000次,如果在每次使用時(shí)直接寫上這幾個(gè)字,這樣下來(lái)程序中將有70000個(gè)字,這70000個(gè)字占136KB的空間。而由于手機(jī)的資源有限,其CPU的處理能力及內(nèi)存是非常有限的,   136KB 對(duì)手機(jī)內(nèi)存來(lái)說是個(gè)不小的空間,我們?cè)谧鍪謾C(jī)應(yīng)用是一定要記住“能省內(nèi)存就省內(nèi)存”。而如果將這幾個(gè)字定義在string.xml中,在每次使用到的地方通過Resources類來(lái)引用該文字,只占用到了14B,因此對(duì)降低應(yīng)用體積效果是非常有效地.當(dāng)然我們可能在開發(fā)時(shí)可能并不會(huì)用到這么多的文字信息,但是,作為手機(jī)應(yīng)用開發(fā)人員,我們一定要養(yǎng)成良好的編程習(xí)慣。


獲取string.xml文件里面的值有幾個(gè)不同的地方。

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或pplication)

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

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



android中string.xml文件的使用

1.在程序中獲取string.xml中字符串和數(shù)值

<?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數(shù)組(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以字節(jié)流的形式讀取文件。

1. 先在Activity里面調(diào)用getAssets() 來(lái)獲取AssetManager引用。

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

3. 然后就是用已經(jīng)open file 的inputStream讀取文件,讀取完成后記得inputStream.close() 。

4.調(diào)用AssetManager.close() 關(guān)閉AssetManager。

需要注意的是,來(lái)自Resources和Assets 中的文件只可以讀取而不能進(jìn)行寫的操作

以下為從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(); 

            }

   

當(dāng)然如果你要得到內(nèi)存流的話也可以直接返回內(nèi)存流!

    本站是提供個(gè)人知識(shí)管理的網(wǎng)絡(luò)存儲(chǔ)空間,所有內(nèi)容均由用戶發(fā)布,不代表本站觀點(diǎn)。請(qǐng)注意甄別內(nèi)容中的聯(lián)系方式、誘導(dǎo)購(gòu)買等信息,謹(jǐn)防詐騙。如發(fā)現(xiàn)有害或侵權(quán)內(nèi)容,請(qǐng)點(diǎn)擊一鍵舉報(bào)。
    轉(zhuǎn)藏 分享 獻(xiàn)花(0

    0條評(píng)論

    發(fā)表

    請(qǐng)遵守用戶 評(píng)論公約

    類似文章 更多