【问题标题】:how to change src attribute dynamically in android studio?如何在android studio中动态更改src属性?
【发布时间】:2018-07-28 18:22:59
【问题描述】:

如何在android studio中动态设置src属性(第7行),我尝试更改backgroundResource但不一样

XML 文件

<ImageView
    android:onClick="next2"
    android:layout_margin="10dp"
    android:id="@+id/img3"
    android:layout_width="60dp"
    android:layout_height="60dp"
    android:background="@drawable/img3"
    android:src="#9d070976"
    android:layout_toEndOf="@+id/img2" />

Java 代码

a.setBackgroundResource(R.color.mycolor);

【问题讨论】:

  • imageView.setImageResource(R.drawable.dynmic_drawble)imageView.setImageDrawble(ContextCompat.getDrawable(context,R.drawable.dynmic_drawble))
  • 我想用一个新值而不是可绘制图像来改变颜色
  • 你必须为此创建 Colordrawable

标签: android


【解决方案1】:

根据您的图像的形式,使用:

如果您需要加载的替换图片是 URL 或 Uri 的形式,请使用 Picasso 或 Glide 等图片加载库。他们可以在背景中加载图像并在图像准备好显示后填写ImageView

【讨论】:

  • 我想通过新值而不是可绘制图像来更改我在 xml 填充中定义的颜色
  • @omarohead:为您想要的颜色创建一个ColorDrawable,然后使用setImageDrawable()
猜你喜欢
  • 1970-01-01
  • 2012-10-09
  • 1970-01-01
  • 2017-03-23
  • 2019-11-14
  • 2017-01-19
  • 1970-01-01
  • 2015-07-16
相关资源
最近更新 更多