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" />
-
參考:
-
執行個體:
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);