在我的应用程序中,我使用以下行来区分一些首选项:
preference.setIcon(new ColorDrawable(color));
在 Lollipop 之前的 Android 版本中,它工作正常,并且首选项显示所选颜色的方形图标,但在 Lollipop 中没有显示。
知道如何解决它吗?
Thanks
这是一个对我有用的解决方案:
preference.setIcon(getPreferenceIcon(color));
function Drawable getPreferenceIcon(int color)
{
if (Build.VERSION.SDK_INT < 21) return new ColorDrawable(color);
int bitmap_size = 64;
Bitmap bitmap = Bitmap.createBitmap(bitmap_size, bitmap_size, Bitmap.Config.ARGB_8888);
Canvas canvas = new Canvas(bitmap);
Paint paint = new Paint();
paint.setColor(color);
canvas.drawRect(new Rect(0, 0, bitmap_size, bitmap_size), paint);
return new BitmapDrawable(getResources(), bitmap);
}
这是《黑客帝国》答案的简化版本,我删除了对该版本的检查,因为它在冰淇淋三明治上也无法正常工作(显示了一条细线,而不是正方形):
private Drawable getPreferenceIcon(int color) {
int size = 200;// Set to a big size to fit all screens, will be contained anyway in the preference row
Bitmap bitmap = Bitmap.createBitmap(size, size, Bitmap.Config.ARGB_8888);
bitmap.eraseColor(color);
return new BitmapDrawable(getResources(), bitmap);
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)