InsetDrawable:内嵌Drawable
更多Drawable详见Android Drawable。
1、InsetDrawable
InsetDrawable将其它Drawable内嵌到自己,在四周预留空白。当视图需要的背景小于视图的实际边界时,使用此方法很有用。当然之前学到的LayerDrawable:叠加图层也可以实现该需求。
2、语法
2.1、语法
InsetDrawable的语法如下:
<?xml version="1.0" encoding="utf-8"?>
<inset
xmlns:android="http://schemas.android.com/apk/res/android"
android:drawable="@drawable/drawable_resource"
android:insetTop="dimension"
android:insetRight="dimension"
android:insetBottom="dimension"
android:insetLeft="dimension" />2.2、<inset>
<inset>定义可绘制的插图,必须是根元素。不像其它的Drawable还有<item>子元素,InsetDrawable可没有,就自已一个Drawable嵌套了其它的一个Drawable。可以选的属性如下:
①android:drawable:可绘制资源,必填。
②android:insetTop:顶部预留,维度值或维度资源id
③android:insetRight:右侧预留
④android:insetBottom:底部预留
⑤android:insetLeft:左侧预留
⑥android:inset:统一四个方向预留相同的空间
3、示例
<inset>定义一个InsetDrawable类型的Drawable资源。
<?xml version="1.0" encoding="utf-8"?>
<inset xmlns:android="http://schemas.android.com/apk/res/android"
android:drawable="@drawable/git"
android:inset="50dp" />作为src设置给ImageView,并且在ImageView设置蓝色的background背景,突出显示
<ImageView
...
android:background="#03DAC5"
android:src="@drawable/inset_drawable" />效果如下

如果不设置inset周围的区域,那么效果如下:

参考资料:

