我在xml中设计了一个图像,并将其放在我的可绘制资源下
ic_receive.xml
<?xml version="1.0" encoding="utf-8"?>
<vector xmlns:android="http://schemas.android.com/apk/res/android"
android:width="24dp"
android:height="24dp"
android:viewportWidth="24"
android:viewportHeight="24">
<path
android:pathData="M0,10.5016l19.5008,0l0,10.4968l-19.5008,0z"
android:strokeWidth="1"
android:fillColor="#C6ECCB"
android:fillType="nonZero"
android:strokeColor="#00000000"/>
<path
android:pathData="M4.5,3.0016l19.5,0l0,10.4968l-19.5,0z"
android:strokeWidth="1"
android:fillColor="#41BF53"
android:fillType="nonZero"
android:strokeColor="#00000000"/>
<path
android:pathData="M18,7.5l-3,0l0,-3l-1.5,0l0,3l-2.9992,0l0,1.5l2.9992,0l0,3l1.5,0l0,-3l3,0z"
android:strokeWidth="1"
android:fillColor="#C6ECCB"
android:fillType="nonZero"
android:strokeColor="#00000000"/>
</vector>
我尝试在 fragment 的回收器 View 中以编程方式设置图像
TransactionsFragment.java
TransactionsModel options = new TransactionsModel("Approved", "$ 3,000", R.drawable.ic_receive, R.drawable.ic_action_home);
transactionsList.add(options);
当我运行应用程序时,ImageView 变成空白,但是当使用 .png 可绘制对象时,它显示正常,就像用于另一个 ImageView 的 R.drawable.ic_action_home 一样。
注意
我还注意到,xml 图像在设置可绘制的代码右侧的小预览中显示良好
像这个( Set background to a drawable programatically )这样的其他教程,似乎指向设置可绘制资源的相同方法,但仍然不适合我。
有人知道为什么会这样吗?我可能错过或遗漏了什么吗?
顺便说一句,如果重要的话,我正在使用新的 AndroidX,并且如果需要的话还可以提供更多代码。
干杯!
编辑
用于在回收器 View 中显示图像的代码
public void onBindViewHolder(MyViewHolder holder, int position) {
TransactionsModel movie = transactionsList.get(position);
Glide.with(mCtx)
.load(movie.getTxIcon())
.into(holder.txIcon);
}
最佳答案
您可以使用此代码 fragment 将可绘制对象设置为背景。
imageView.setBackgroundResource(R.drawable.bgg);
imageView.setBackgroundDrawable(R.drawable.bgg);
关于java - 如何以编程方式将 XML 可绘制对象设置为背景图像?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59585875/