zoukankan      html  css  js  c++  java
  • 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的值

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

    }

    }

    当然如果你要得到内存流的话也可以直接返回内存流!

  • 相关阅读:
    lucene复合条件查询案例——查询name域 或 description域 包含lucene关键字的两种方式
    谈谈你对spring的理解?
    模拟Http请求的几种常用方式
    idea破解教程
    Pycharm 或者 idea 注册码 (亲测可用)
    超详细虚拟机工具和Centos7 Linux系统安装
    严重: Error loading WebappClassLoader context:异常的解决方法(转)
    spring cloud 笔记记录
    近一周学习之-----vue学习快乐之旅(1)
    近一周学习之-----vue开发工具visual studio code
  • 原文地址:https://www.cnblogs.com/johnpher/p/2883687.html
Copyright © 2011-2022 走看看