gpt4 book ai didi

android - 如何使用数据绑定(bind)在android中动态设置 ImageView 的图像资源

转载 作者:行者123 更新时间:2023-12-04 23:44:33 25 4
gpt4 key购买 nike

有人可以让我知道使用数据绑定(bind)在 ImageView 中填充图像资源的正确用法吗

最佳答案

你可以引用这个:
步骤1
在这样的文件中创建一个方法

@BindingAdapter("set_expense_category_image")
fun ImageView.setImageResource(expenseCategory: String) {
this.setImageResource(
when (expenseCategory) {
ExpenseTypes.FOOD.expenseLitral -> R.drawable.ic_baseline_fastfood_24
ExpenseTypes.SHOPPING.expenseLitral -> R.drawable.ic_baseline_shopping_basket_24
ExpenseTypes.GYM.expenseLitral -> R.drawable.ic_baseline_accessibility_new_24
ExpenseTypes.MEDICAL.expenseLitral -> R.drawable.ic_baseline_medical_services_24
ExpenseTypes.HOUSE_RENT.expenseLitral -> R.drawable.ic_baseline_house_24
ExpenseTypes.TRAVEL.expenseLitral -> R.drawable.ic_baseline_emoji_transportation_24
ExpenseTypes.FREE_HAND_MONEY.expenseLitral -> R.drawable.ic_outline_money_24
ExpenseTypes.INVESTING.expenseLitral -> R.drawable.ic_baseline_monetization_on_24
ExpenseTypes.MONTHLY_EMI.expenseLitral -> R.drawable.ic_baseline_payments_24
ExpenseTypes.MISCELLANEOUS.expenseLitral -> R.drawable.ic_baseline_kitesurfing_24
else -> R.drawable.ic_baseline_supervisor_account_24
}
)
}
第2步
您可以在 ImageView 中使用基于输入加载动态图像,请参阅以下内容:
<ImageView
android:id="@+id/imageView4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
set_expense_category_image="@{expense.expenseCategoryName}"
app:layout_constraintBottom_toTopOf="@+id/view"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="@+id/imageView5"
app:srcCompat="@drawable/ic_baseline_fastfood_24" />

关于android - 如何使用数据绑定(bind)在android中动态设置 ImageView 的图像资源,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/70541212/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com