Android之Drawable-InsetDrawable

注意:本篇文章是本人阅读相关文章所写下的总结,方便以后查阅,所有内容非原创,侵权删。

本篇文章内容来自于:
1.Android开发艺术探索 任玉刚
2.领略千变万化的Android Drawable (一)

目录

  1. InsetDrawable
    --10.1 什么是InsetDrawable
    --10.2 InsetDrawable语法/属性
    --10.3 InsetDrawable使用案例

10. InsetDrawable

10.1 什么是InsetDrawable

InsetDrawable对应标签<inset>,它可以将其他Drawable内嵌到自己当中,并可以在四周预留出一定的间距。
当我们希望View的背景比实际区域小时,就可以采用InsetDrawable来实现。
layerDrawable也是可以达到相同的预期效果的。

10.2 InsetDrawable语法/属性

<?xml version="1.0" encoding="utf-8"?>
<inset
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:drawable="@drawable/drawable_resource"
    //android:insetTop  图像距离上边的距离
    //android:insetRight    图像距离右边的距离
    //android:insetBottom   图像距离底边的距离
    //android:insetLeft 图像距离左边的距离
    android:insetTop="dimension"
    android:insetRight="dimension"
    android:insetBottom="dimension"
    android:insetLeft="dimension" />

10.3 InsetDrawable使用案例

大致效果:


第一步:定义xml

<?xml version="1.0" encoding="utf-8"?>
<inset xmlns:android="http://schemas.android.com/apk/res/android"
    android:insetBottom="10dp"
    android:drawable="@drawable/transition_bg_1"
    android:insetTop="10dp"
    android:insetLeft="10dp"
    android:insetRight="10dp"
    >
</inset>

也可以写成

<?xml version="1.0" encoding="utf-8"?>
<inset xmlns:android="http://schemas.android.com/apk/res/android"
    android:insetBottom="10dp"
    android:insetLeft="10dp"
    android:insetRight="10dp"
    android:insetTop="10dp">
    <shape android:shape="rectangle">
        <solid android:color="@color/colorPrimary" />
    </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"
    android:background="@drawable/inset_drawable"
    tools:context="com.zejian.drawble.MainActivity">
 
</RelativeLayout>
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容