Android: integer from xml resource

馋奶兔 提交于 2019-11-27 11:37:33
Terry

Yes it is possible, it would look like this:

  1. Create an xml resources file in the folder /res/values/ called integers.xml.

    You are free to give it any name as you want, but choose one that is obvious.

  2. In that resources file, create your integer values.

    Your file then looks something like that:

    <?xml version="1.0" encoding="utf-8"?>
    <resources>    
        <integer name="maximum">100</integer>
        ...
    
    </resources>
    
  3. Reference the integer value in the Java code like this:

    It's a bit different from the getString(), you have to take a little detour.

    ProgressDialog progressBar = new ProgressDialog(getContext());
    int max = getContext().getResources().getInteger(R.integer.maximum);
    progressBar.setMax(max);
    

Hope that helps anybody who has the same question.

You must add the integers.xml file to your project

and then

and in integers.xml add this

<integer name="maximum">5</integer>
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!