我需要通过@ + id / calendar_day_bottom_icon以编程方式在图层列表中设置不同的图标
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="@drawable/calendar_date_today_bg"/>
<item android:id="@+id/calendar_day_bottom_icon" >
<bitmap
android:src="@drawable/ic_calendar_journal"
android:gravity="bottom|center" />
</item>
</layer-list>
在代码中我做下一件事
LayerDrawable layerDrawable = (LayerDrawable) cellView.getBackground();
BitmapDrawable drawable = (BitmapDrawable) getResources().getDrawable(R.drawable.ic_calendar_journal_reminder);
layerDrawable.setDrawableByLayerId(R.id.calendar_day_bottom_icon, drawable);
但图标插入不正确。
答案 0 :(得分:0)
我找到了另一个可绘制文件的解决方案:
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item>
<bitmap
android:gravity="bottom"
android:src="@drawable/ic_calendar_journal" />
</item>
</layer-list>
得到可绘制的
LayerDrawable layerDrawable = (LayerDrawable) ContextCompat.getDrawable(context, R.drawable.calendar_layers);
获取LayerDrawable,获取图层项并将其设置为另一个LayerDrawable。
LayerDrawable cellLayerDrawable = (LayerDrawable) cellView.getBackground();
Drawable drawable = layerDrawable.getDrawable(0);
cellLayerDrawable.setDrawableByLayerId(R.id.calendar_day_bottom_icon, drawable);