如何将DrawableId参数传递到XML布局中?

huangapple go评论59阅读模式
英文:

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
&lt;layout xmlns:android=&quot;http://schemas.android.com/apk/res/android&quot;
    xmlns:app=&quot;http://schemas.android.com/apk/res-auto&quot;&gt;
    &lt;androidx.constraintlayout.widget.ConstraintLayout
        android:id=&quot;@+id/item&quot;
        android:layout_width=&quot;match_parent&quot;
        android:layout_height=&quot;wrap_content&quot;&gt;
        &lt;ImageView
            android:id=&quot;@+id/icon&quot;
            android:layout_width=&quot;24dp&quot;
            android:layout_height=&quot;24dp&quot;
            android:src=&quot;&quot;
            app:layout_constraintBottom_toBottomOf=&quot;parent&quot;
            app:layout_constraintStart_toStartOf=&quot;parent&quot;
            app:layout_constraintTop_toTopOf=&quot;parent&quot;/&gt;
    &lt;/androidx.constraintlayout.widget.ConstraintLayout&gt;
&lt;/layout&gt;

Image view's src must come from include parameter like "@drawable/icon":

&lt;include
   layout=&quot;@layout/item&quot;
   app:drawableIdParameter=&quot;@drawable/icon&quot;/&gt;

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:

&lt;data&gt;
    &lt;import type=&quot;android.graphics.drawable.Drawable&quot; /&gt;
    &lt;variable
        name=&quot;drawableIdParameter&quot;
        type=&quot;Drawable&quot;/&gt;
&lt;/data&gt;

Access the variable in the ImageView as:

android:src=&quot;@{drawableIdParameter}&quot;

Now pass the parameter in the following way:

&lt;include
    layout=&quot;@layout/item&quot;
    app:drawableIdParameter=&quot;@{@drawable/icon}&quot;/&gt;

huangapple
  • 本文由 发表于 2023年7月3日 19:05:50
  • 转载请务必保留本文链接:https://go.coder-hub.com/76604152.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定