跳至主要内容

android获取string.xml的值

为什么需要把应用中出现的文字单独存放在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或pplication)
方法一: 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(); 
            }
    } 
当然如果你要得到内存流的话也可以直接返回内存流!

评论

此博客中的热门博文

android hide actionbar

public class MainActivity extends Activity { ActionBar actionBar; //声明ActionBar @Override protected void onCreate( Bundle savedInstanceState) { super .onCreate(savedInstanceState); setContentView( R .layout.activity_main); actionBar = getSupportActionBar(); //得 到ActionBar actionBar.hide(); //隐藏ActionBar } }

go url encoding

func  QueryUnescape func QueryUnescape (s string ) ( string , error ) QueryUnescape does the inverse transformation of QueryEscape, converting %AB into the byte 0xAB and '+' into ' ' (space). It returns an error if any % is not followed by two hexadecimal digits. func  QueryUnescape func QueryUnescape (s string ) ( string , error ) QueryUnescape does the inverse transformation of QueryEscape, converting %AB into the byte 0xAB and '+' into ' ' (space). It returns an error if any % is not followed by two hexadecimal digits.