如何在Android中使用XML资源数组?

23

我是Android开发的新手,遇到了管理Android资源的问题。我想创建一个带有ImageView和TextView的listView。

以下是我的实现方式,它可以正常工作,但是实际上我想使用之前创建的数组,如下所示:

int[] img = getResources().getIntArray(R.Array.img);
package com.simplelistviewwithlistactivity;

import android.app.AlertDialog;
import android.app.AlertDialog.Builder;
import android.app.ListActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.widget.ListView;

public class ListActivityS extends ListActivity {
    int[] img = { R.drawable.r1, R.drawable.r2, R.drawable.skycubemap1,
            R.drawable.skycubemap1, R.drawable.skycubemap2,
            R.drawable.skycubemap3, R.drawable.skycubemap4,
            R.drawable.skycubemap5 };

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        getListView().setDividerHeight(2);
        getListView().setAdapter(new BindDataAdapter(this, img, item));
    }

    @Override
    protected void onListItemClick(ListView l, View v, int position, long id) {
        super.onListItemClick(l, v, position, id);
        Builder builder = new AlertDialog.Builder(this);
        builder.setMessage(item[position] + " is clicked.");
        builder.setPositiveButton("OK", null);
        builder.show();
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.activity_list, menu);
        return true;
    }

    private String item[] = { "This is list Item1", "This is list Item2",
            "This is list Item3", "This is list Item4", "This is list Item5",
            "This is list Item6", "This is list Item8", "This is list Item8"

您可以按照以下示例在数组中使用颜色:https://dev59.com/sWox5IYBdhLWcg3wk1Ig#17584066 - Sky Kelsey
嗨,Jannis,你是否发现任何给出的答案有用来解决你的问题?如果是,请考虑标记该答案为已接受。你的小举动将帮助其他人找到类似问题的解决方案。 - sourav.bh
2个回答

51
在res/values/arrays.xml中创建以下类似于XML的内容。
<?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>

然后使用以下代码:

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

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

来源: http://developer.android.com/guide/topics/resources/more-resources.html

这个链接提供了更多关于Android开发资源的信息。


2
在使用完毕后,不要忘记调用iconscolorsrecycle()方法。 - CoolMind

18

你可以使用来自 res/values/arrays.xml 的资源。

对于可绘制资源

<integer-array name="your_images">
    <item>@drawable/ic_active_image</item>
    <item>@drawable/ic_visited_image</item>
</integer-array>

val position = 1 // Position in array.
val drawables = resources.obtainTypedArray(R.array.your_images)
val drawable = drawables.getResourceId(position, -1)
image.setImageResource(drawable)
drawables.recycle()

关于颜色

<array name="your_colors">
    <item>#365374</item>
    <item>#00B9FF</item>
</array>

val position = 1
val colors = resources.obtainTypedArray(R.array.your_colors)
val color = colors.getColor(position, -1)
title.setTextColor(color)
colors.recycle()

关于字符串数组

<string-array name="your_strings">
    <item>Active</item>
    <item>Visited</item>
</string-array>

val position = 1
val strings = resources.getStringArray(R.array.your_strings)
title.text = strings[position]

复数形式

<plurals name="proposal_plurals">
    <item quantity="zero">No proposals</item>
    <item quantity="one">%1$d proposal</item>
    <item quantity="two">%1$d proposals</item>
    <item quantity="few">%1$d proposals</item>
    <item quantity="many">%1$d proposals</item>
    <item quantity="other">%1$d proposals</item>
</plurals>

val count = 117
val proposals = count.takeIf { it != 0 }?.let {
    resources.getQuantityString(R.plurals.proposal_plurals, it, it)
} ?: "No proposals"

你的颜色代码看起来好像你也可以直接在资源上使用getColor(),这样就可以省去recycle()的步骤。是这样吗? - undefined
@TheincredibleJan,为什么不呢?这是一个常见的情况。在Kotlin中,我们使用use操作符来替代。 - undefined

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接