【问题标题】:Storing R.drawable IDs in XML array在 XML 数组中存储 R.drawable ID
【发布时间】:2023-03-05 23:52:02
【问题描述】:

我想使用 XML 值文件将可绘制资源的 ID 以 R.drawable.* 的形式存储在数组中,然后在我的活动中检索该数组。

关于如何实现这一点的任何想法?

【问题讨论】:

  • 你能解释一下“在数组中使用 XML”是什么意思吗?
  • 一个值文件。例如,strings.xml
  • 我不明白你为什么要这样做。您能否提供更多背景信息来说明您为什么要这样做?
  • 听起来您正在尝试做一些比必要的更复杂的事情。
  • 我要做的是存储将在列表视图中显示的图像的 id。

标签: android arrays android-xml android-drawable android-resources


【解决方案1】:

您在/res/values 文件夹中的arrays.xml 文件中使用typed array,如下所示:

<?xml version="1.0" encoding="utf-8"?>
<resources>

    <integer-array name="random_imgs">
        <item>@drawable/car_01</item>
        <item>@drawable/balloon_random_02</item>
        <item>@drawable/dog_03</item>
    </integer-array>

</resources>

然后在您的活动中,像这样访问它们:

TypedArray imgs = getResources().obtainTypedArray(R.array.random_imgs);

// get resource ID by index, use 0 as default to set null resource
imgs.getResourceId(i, 0)

// or set you ImageView's resource to the id
mImgView1.setImageResource(imgs.getResourceId(i, 0));

// recycle the array
imgs.recycle();

【讨论】:

  • 嘿,你能解释一下 imgs.getResourceId(i,-1) 中的 -1 代表什么吗?
  • 建议:在用户“imgs”后添加以下行:imgs.recycle();
  • 您应该使用 0 而不是 -1 作为默认 ID。 -1 是有效的资源 ID,而 0 是空资源。
  • 太棒了!类型化数组很不错。
  • 使用 length() 获取 TypedArray 的元素计数。
【解决方案2】:

value文件夹中创建xml文件名arrays.xml 以这种方式向其中添加数据

<integer-array name="your_array_name">
    <item>@drawable/1</item>
    <item>@drawable/2</item>
    <item>@drawable/3</item>
    <item>@drawable/4</item>
</integer-array>

然后以这种方式将其获取到您的代码中

private TypedArray img;
img = getResources().obtainTypedArray(R.array.your_array_name);

然后在img TypedArray 中使用其中的Drawable 作为ImageView background 使用以下代码

ImageView.setBackgroundResource(img.getResourceId(index, defaultValue));

其中indexDrawable 索引。 defaultValue 是您在此 index 中没有项目时给出的值

有关TypedArray的更多信息,请访问此链接 http://developer.android.com/reference/android/content/res/TypedArray.html

【讨论】:

    【解决方案3】:

    您可以使用它来创建其他资源的数组,例如可绘制对象。请注意,数组不需要是同质的,因此您可以创建混合资源类型的数组,但您必须知道数据类型在数组中的内容和位置。

     <?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>
    

    然后像这样获取你activity中的资源

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

    享受!!!!!!

    【讨论】:

      【解决方案4】:

      在 Kotlin 中,您可以这样做:-

       <integer-array name="drawer_icons">
          <item>@drawable/drawer_home</item>
      </integer-array>
      

      您将从资源中获取图像数组TypedArray

       val imageArray = resources.obtainTypedArray(R.array.drawer_icons)
      

      通过索引获取资源ID

      imageArray.getResourceId(imageArray.getIndex(0),-1)
      

      或者你可以将imageView的资源设置为id

      imageView.setImageResource(imageArray.getResourceId(imageArray.getIndex(0),-1))
      

      最后回收阵列

      imageArray.recycle()
      

      【讨论】:

        【解决方案5】:

        kotlin 方式可能是这样的:

        fun Int.resDrawableArray(context: Context, index: Int, block: (drawableResId: Int) -> Unit) {
          val array = context.resources.obtainTypedArray(this)
          block(array.getResourceId(index, -1))
          array.recycle()
        }
        
        R.array.random_imgs.resDrawableArray(context, 0) {
          mImgView1.setImageResource(it)
        }
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2012-06-16
          • 2012-10-18
          • 2019-10-30
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多