InsetDrawable 表示一个drawable根据指定的距离嵌入到另外一个drawable内部。(我们看到的其实还是同一张图片,只是会空出一些边距)当控件需要的背景比实际的边框小的时候比较适合使用InsetDrawable。
很像drawable的padding属性,区别在于 padding表示drawable的内容与drawable本身的边距,insetDrawable表示两个drawable和容器之间的边距。
语法:
元素
<inset>
定义这个drawable为InsetDrawable,必须作为根节点。
属性:
xmlns:android
String类型。必须的,定义XML文件的命名空间,必须是"http://schemas.android.com/apk/res/android".
android:drawable
Drawable 资源 。必须的。引用一个drawable资源
android:insetTop
尺寸。与顶部的距离。可以使一个尺寸值,或者一个尺寸的资源。
android:insetRight
尺寸。与右边的距离。可以使一个尺寸值,或者一个尺寸的资源。
android:insetBottom
尺寸。与底部的距离。可以使一个尺寸值,或者一个尺寸的资源。
android:insetLeft
尺寸。与左边的距离。可以使一个尺寸值,或者一个尺寸的资源。
在drawab下自定义一个insetdrawable.xml
<?xml version="1.0" encoding="utf-8"?>
<inset xmlns:android="http://schemas.android.com/apk/res/android"
android:drawable="@drawable/onepiece"
android:insetBottom="20dp"
android:insetLeft="20dp"
android:insetRight="20dp"
android:insetTop="20dp">
</inset>
在布局中引用
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context="com.example.clipdrawable.MainActivity">
<ImageView
android:id="@+id/clipimageview"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/insetdrawable"/>
</LinearLayout>
显示的效果
转载地址