开发者

Creating a list of integers in XML for android

开发者 https://www.devze.com 2022-12-28 15:01 出处:网络
I would like to create a list of Integers in the /res folder of an android project.However, I want those integers to point resources in /res/raw.So for example, I would like something like this:

I would like to create a list of Integers in the /res folder of an android project. However, I want those integers to point resources in /res/raw. So for example, I would like something like this:

<?xml version="1.0" encoding="utf-8"?>
    <resources>
        <integer-array name="built_in_sounds">
            <item>@raw/sound</item>
    </integer-array>
</开发者_运维技巧resources>

But id doesn't look like I can do that, is there any way to do this? Or should I just create the list in a java class?

Thank you


And the correct answer is actually, TypedArray

The documentation shows examples of lists of resources:

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <array name="icons">
        <item>@drawable/home</item>
        <item>@drawable/settings</item>
        <item>@drawable/logout</item>
    </array>
    <array name="colors">
        <item>#FFFF0000</item>
        <item>#FF00FF00</item>
        <item>#FF0000FF</item>
    </array>
</resources>

And the code to retrieve the values:

Resources res = getResources();
TypedArray icons = res.obtainTypedArray(R.array.icons);
Drawable drawable = icons.getDrawable(0);

TypedArray colors = res.obtainTypedArray(R.array.icons);
int color = colors.getColor(0,0);


Okay, I finally found out how to do this. What I did, was just create a database. The database stored all of the primitives I needed to store, and than pointers to the objects that I needed to reference. Apparently the android SDK comes with support for SQLite.


To do this in XML without a database, see: http://developer.android.com/guide/topics/resources/more-resources.html#Integer

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号