InsetDrawable 表示一个drawable嵌入到另外一个drawable内部,并且在内部留一些间距,这一点很像drawable的padding属性,区别在于 padding表示drawable的内容与drawable本身的边距,insetDrawable表示两个drawable和容器之间的边距。当控件需要的背景比实际的边框小的时候比较适合使用InsetDrawable。
文件位置:
res/drawable/filename.xml
文件 名即资源ID
编译资源类型:
指向InsetDrawable
的指针
资源引用:
In Java: R.drawable.filename
In XML: @[package:]drawable/filename
语法:
<?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" />
元素
<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
尺寸。与左边的距离。可以使一个尺寸值,或者一个尺寸的资源。
例子:
<?xml version="1.0" encoding="utf-8"?>
<inset xmlns:android="http://schemas.android.com/apk/res/android"
android:drawable="@drawable/background"
android:insetTop="10dp"
android:insetLeft="10dp" />
参考:
InsetDrawable
实例:
1、在xml文件中定义
inset.xml:
-
<?xml version="1.0" encoding="utf-8"?>
-
<inset xmlns:android="http://schemas.android.com/apk/res/android"
-
android:drawable="@drawable/image4"
-
android:insetLeft="50dp"
-
android:insetRight="50dp"
-
android:insetTop="20dp"
-
android:insetBottom="20dp">
-
</inset>
layout中使用:
-
<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"
-
android:background="@drawable/inset" >
-
<Button
-
android:id="@+id/btn"
-
android:layout_width="wrap_content"
-
android:layout_height="wrap_content"/>
-
</LinearLayout>
可以看到,它与边界之间是有间距的。就是我们在inset.xml中所定义的。
2、java代码定义:
很简单:
-
//new InsetDrawable(drawable, insetLeft, insetTop, insetRight, insetBottom)
-
InsetDrawable insetDrawable=new InsetDrawable(getResources().getDrawable(R.drawable.image4), 20, 30, 30, 20);