英文:
How can I pass the DrawableId parameter to XML Layout?
问题
我有一个像这样的布局item.xml:
```xml
<layout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<androidx.constraintlayout.widget.ConstraintLayout
android:id="@+id/item"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<ImageView
android:id="@+id/icon"
android:layout_width="24dp"
android:layout_height="24dp"
android:src=""
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"/>
</androidx.constraintlayout.widget.ConstraintLayout>
</layout>
ImageView的src必须来自包含参数,如"@drawable/icon":
<include
layout="@layout/item"
app:drawableIdParameter="@drawable/icon"/>
如何将drawable id参数传递到布局?
<details>
<summary>英文:</summary>
I have an layout item.xml like this:
```xml
<layout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<androidx.constraintlayout.widget.ConstraintLayout
android:id="@+id/item"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<ImageView
android:id="@+id/icon"
android:layout_width="24dp"
android:layout_height="24dp"
android:src=""
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"/>
</androidx.constraintlayout.widget.ConstraintLayout>
</layout>
Image view's src must come from include parameter like "@drawable/icon":
<include
layout="@layout/item"
app:drawableIdParameter="@drawable/icon"/>
How can I pass drawable id parameter to layout?
答案1
得分: 1
在你的item.xml中添加类型为Drawable
的变量:
<data>
<import type="android.graphics.drawable.Drawable" />
<variable
name="drawableIdParameter"
type="Drawable"/>
</data>
在ImageView
中访问这个变量:
android:src="@{drawableIdParameter}"
现在以以下方式传递参数:
<include
layout="@layout/item"
app:drawableIdParameter="@{@drawable/icon}"/>
英文:
In your item.xml add the variable of type Drawable
:
<data>
<import type="android.graphics.drawable.Drawable" />
<variable
name="drawableIdParameter"
type="Drawable"/>
</data>
Access the variable in the ImageView
as:
android:src="@{drawableIdParameter}"
Now pass the parameter in the following way:
<include
layout="@layout/item"
app:drawableIdParameter="@{@drawable/icon}"/>
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论