本文出自 “阿敏其人” 簡書博客,轉載或引用請注明出處。
InsetDrawable對應的標簽是<inset>
他可以將其他的Drawable內嵌到自己的里面。個人覺得其實沒什么用,他能做到的,LayerDrawable也能做,或者有的時候直接設置一下padding就可以了。
一、語法
<?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" />
沒什么好說的,四個方向就是想要拉開的距離,比如一個圖片本來應該是占滿屏幕的,然后上下左右個設置為20dp,那么待會這個圖片就不會占滿屏幕,就是距離上下左右都有20dp的距離。
二、demo
insetdrawable_simple.xml
<?xml version="1.0" encoding="utf-8"?>
<inset xmlns:android="http://schemas.android.com/apk/res/android"
android:insetBottom="60dp"
android:insetLeft="30dp"
android:insetRight="30dp"
android:insetTop="60dp" >
<!--待會要插入的一個藍色的矩形-->
<shape android:shape="rectangle" >
<solid android:color="#0000ff" />
</shape>
</inset>
.
.
布局文件
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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"
tools:context=".MainActivity">
<!--Inset這東西其實就沒什么用,大概也就這樣子吧,
背景一個,圖片一個,利用Inset讓背景比較大-->
<ImageView
android:id="@+id/mIv"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_centerInParent="true"
android:background="@mipmap/op"
android:src="@drawable/insetdrawable_simple"
/>
</RelativeLayout>
.
.
Activity代碼什么都不要動
import android.app.Activity;
import android.os.Bundle;
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//InsetDrawable insetDrawable=new InsetDrawable(getResources().getDrawable(R.drawable.XXX), 20, 30, 30, 20);
}
}
.
.
demo效果:
demo效果.png
了解更多的Drawable分類 Drawable圖像資源抽象類
本篇完。