在 Kotlin 的 xml 数组中在 ImageButton 上设置 mipmap 图像?

Posted

技术标签:

【中文标题】在 Kotlin 的 xml 数组中在 ImageButton 上设置 mipmap 图像?【英文标题】:Setting up mipmap images on ImageButton from xml array in Kotlin? 【发布时间】:2020-07-16 08:27:48 【问题描述】:

所以我在一个包含@mipmap 图标的xml 值中定义了一个数组。我正在尝试从它们那里获取图标 int 值并将它们设置在 ImageButton 上。但是,每当我尝试从它们那里获取 int 值时,如果我将它设为“整数数组”,我会得到 0,如果它只是一个“数组”,则会得到 Resources$NotFoundException。有什么解决办法吗?

mainActivity.kt

private fun setImage() 
    val iconArray = resources.getIntArray(R.array.transaction_icon_array)
    val imageView: ImageView = findViewById(R.id.image_icon)
    imageView.setImageResource(resources.getInteger(iconArray[0]))
    Log.d("ABID", imageView.tag.toString())

transaction_icon_array.xml

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <integer-array name="transaction_icon_array">
        <item>@mipmap/transaction_icon0</item>
        <item>@mipmap/transaction_icon1</item>
        <item>@mipmap/transaction_icon2</item>
        <item>@mipmap/transaction_icon3</item>
        <item>@mipmap/transaction_icon4</item>
        <item>@mipmap/transaction_icon5</item>
        <item>@mipmap/transaction_icon6</item>
        <item>@mipmap/transaction_icon7</item>
        <item>@mipmap/transaction_icon8</item>
        <item>@mipmap/transaction_icon9</item>
        <item>@mipmap/transaction_icon10</item>
        <item>@mipmap/transaction_icon11</item>
        <item>@mipmap/transaction_icon12</item>
        <item>@mipmap/transaction_icon13</item>
        <item>@mipmap/transaction_icon14</item>
        <item>@mipmap/transaction_icon15</item>
        <item>@mipmap/transaction_icon16</item>
        <item>@mipmap/transaction_icon17</item>
        <item>@mipmap/transaction_icon18</item>
        <item>@mipmap/transaction_icon19</item>
        <item>@mipmap/transaction_icon20</item>
        <item>@mipmap/transaction_icon21</item>
        <item>@mipmap/transaction_icon22</item>
        <item>@mipmap/transaction_icon23</item>
        <item>@mipmap/transaction_icon24</item>
        <item>@mipmap/transaction_icon25</item>
        <item>@mipmap/transaction_icon26</item>
        <item>@mipmap/transaction_icon27</item>
        <item>@mipmap/transaction_icon28</item>
        <item>@mipmap/transaction_icon29</item>
        <item>@mipmap/transaction_icon30</item>
        <item>@mipmap/transaction_icon31</item>
        <item>@mipmap/transaction_icon32</item>
        <item>@mipmap/transaction_icon33</item>
        <item>@mipmap/transaction_icon34</item>
        <item>@mipmap/transaction_icon35</item>
        <item>@mipmap/transaction_icon36</item>
        <item>@mipmap/transaction_icon37</item>
        <item>@mipmap/transaction_icon38</item>
        <item>@mipmap/transaction_icon39</item>
        <item>@mipmap/transaction_icon40</item>
        <item>@mipmap/transaction_icon41</item>
        <item>@mipmap/transaction_icon42</item>
        <item>@mipmap/transaction_icon43</item>
        <item>@mipmap/transaction_icon44</item>
        <item>@mipmap/transaction_icon45</item>
        <item>@mipmap/transaction_icon46</item>
        <item>@mipmap/transaction_icon47</item>
        <item>@mipmap/transaction_icon48</item>
        <item>@mipmap/transaction_icon49</item>
        <item>@mipmap/transaction_icon50</item>
    </integer-array>
</resources>

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<layout>
    <data>
        <variable
            name="viewModel"
            type="com.example.project_budget_planner.main.transaction.add_transaction.AddTransactionViewModel" />
    </data>
    <androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        xmlns:tools="http://schemas.android.com/tools"
        android:layout_
        android:layout_
        tools:context=".main.transaction.add_transaction.AddTransactionActivity">

        <androidx.appcompat.widget.Toolbar
            android:id="@+id/toolbar"
            android:layout_
            android:layout_
            style="@style/AppTheme.Toolbar"
            android:onClick="@() -> viewModel.navigateUp()"
            app:navigationIcon="@drawable/back_icon"
            app:layout_constraintTop_toTopOf="parent"
            app:layout_constraintStart_toStartOf="parent">

            <TextView
                android:id="@+id/header"
                android:layout_
                android:layout_
                style="@style/AppTheme.ToolbarHeader"
                android:text="@string/new_transaction_header"/>

        </androidx.appcompat.widget.Toolbar>

        <ImageButton
            android:id="@+id/image_icon"
            android:tag="18"
            android:layout_
            android:layout_
            app:layout_constraintBottom_toBottomOf="parent"/>

    </androidx.constraintlayout.widget.ConstraintLayout>
</layout>

【问题讨论】:

【参考方案1】:

你使用 TypedArray 代替使用 getIntArray

val typedArr = getResources().obtainTypedArray(R.array.transaction_icon_array);

//getResourceId(int index, int defValue)
typedArr.getResourceId(index, -1) // You can get resource ID by index

imageView.setImageResource(imgs.getResourceId(index, -1)); // set mipmap to your ImageView

【讨论】:

以上是关于在 Kotlin 的 xml 数组中在 ImageButton 上设置 mipmap 图像?的主要内容,如果未能解决你的问题,请参考以下文章

在 ViewSwitcher 中在不确定的 ProgressBar 和 Image 之间切换

转角器 videoview kotlin

如何通过单击 Image Kotlin Android 来移动 Activity

如何查看或解码 .kotlin_module

在 Cocoapod 中导入 Kotlin/Native 框架

如何使用 Kotlin 在所有活动中播放背景音乐?