does anyone have tried to set 9Patch background of button dynamically? If it is important, button's width and height is set wrap_content
If yes, how have you solved the "black line" issue?
Thanks
does anyone have tried to set 9Patch background of button dynamically? If it is important, button's width and height is set wrap_content
If yes, how have you solved the "black line" issue?
Thanks
如果您看到您在 9-patch 图像上绘制的黑点,那是因为您没有预编译您的图像。
要在运行时将 9-patch 图像设置为背景,必须在移除边框并将其编码为 PNG 块时预先编译它们。
您可以通过在res/drawable
应用程序的文件夹中插入 .9.png 图像、编译它并生成 .apk 来做到这一点(在 Eclipse 上,右键单击您的project root > Android Tools > Export Unsigned Application Package
. 然后解压缩 .apk,您将获得 .9.png 图像,其中包含 9-patch 编译数据。
使用预编译的 9-patch 图像,执行类似的操作来加载图像:
private Drawable loadNinePatch(String path, Context context) {
Bitmap bitmap = BitmapFactory.decodeFile(path);
byte[] chunk = bitmap.getNinePatchChunk();
if(NinePatch.isNinePatchChunk(chunk)) {
return new NinePatchDrawable(context.getResources(), bitmap, chunk, new Rect(), null);
} else return new BitmapDrawable(bitmap);
}
然后将其设置为按钮的背景:
button.setBackgroundDrawable(loadNinePatch("/data/data/your.app/files/button_background.9.png", context));