Insetdrawable indicates that a drawable is embedded into another drawable, and some padding is left inside it. This is similar to the padding attribute of drawable. The difference is that padding indicates the margin between drawable content and drawable, insetdrawable indicates the margin between two drawable and container. When the background required by the control is smaller than the actual border, insetdrawable is more suitable.
-
File Location:
-
res/drawable/filename.xml
The file name is the resource ID.
-
Compilation Resource Type:
-
Point to insetdrawablePointer
-
Resource reference:
-
In Java:
R.drawable.filename
In XML: @[package:]drawable/filename
-
Syntax:
-
<?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" />
-
Element
-
-
<inset>
-
Define this drawable as insetdrawable and must be used as the root node.
Attribute:
-
xmlns:android
-
StringType. Required. The namespace that defines the XML file must be
"http://schemas.android.com/apk/res/android".
-
android:drawable
-
Drawable Resource. Required. Reference A drawable Resource
-
android:insetTop
-
Size. The distance from the top. You can set a dimension value or a dimension resource.
-
android:insetRight
-
Size. The distance from the right side. You can set a dimension value or a dimension resource.
-
android:insetBottom
-
Size. The distance from the bottom. You can set a dimension value or a dimension resource.
-
android:insetLeft
-
Size. The distance from the left. You can set a dimension value or a dimension resource.
-
Example:
-
<?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" />
-
Refer:
-
Instance:
1. Define in XML file
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>
Used in 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>
We can see that there is a gap between it and the boundary. It is defined in inset. xml.
2. Java code definition:
Simple:
//new InsetDrawable(drawable, insetLeft, insetTop, insetRight, insetBottom) InsetDrawable insetDrawable=new InsetDrawable(getResources().getDrawable(R.drawable.image4), 20, 30, 30, 20);