Android Drawable之InsetDrawable

Overview

InsetDrawable 表示一个drawable根据指定的距离嵌入到另外一个drawable内部。(我们看到的其实还是同一张图片,只是会空出一些边距)当控件需要的背景比实际的边框小的时候比较适合使用InsetDrawable。

创建和使用

在xml文件中使用<inset>作为根节点来定义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="dimension"
    android:insetRight="dimension"
    android:insetBottom="dimension"
    android:insetLeft="dimension" />
 
 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8

元素: 
<inset> 
定义这个drawable为InsetDrawable,必须作为根节点。

  • android:drawable Drawable 资源 。必须的。引用一个drawable资源
  • android:visible 设置Drawable是否可见
  • android:insetTop 尺寸。与顶部的距离。可以是一个尺寸值,或者一个尺寸的资源。
  • android:insetRight 尺寸。与右边的距离。可以是一个尺寸值,或者一个尺寸的资源。
  • android:insetBottom 尺寸。与底部的距离。可以是一个尺寸值,或者一个尺寸的资源。
  • android:insetLeft 尺寸。与左边的距离。可以是一个尺寸值,或者一个尺寸的资源。
  • android:inset 尺寸。当上下左右距离一致的时候,使用android:inset可以简化设置。(要求API level 大于21)

<inset>节点下也可以添加<shape>节点,例如:

<?xml version="1.0" encoding="utf-8"?>  
<inset xmlns:android="http://schemas.android.com/apk/res/android"  
    android:inset="10dp">  
    <shape>  
        <solid android:color="@color/red" />  
        <padding android:top="2dp" android:bottom="2dp"   
                 android:left="8dp" android:right="8dp"  />  
    </shape>  
</inset>
 
 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9

对比下面两张图,上面是将图片直接设置为背景:

<RelativeLayout 
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@drawable/onboarding_slide4">
 
 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 1
  • 2
  • 3
  • 4
  • 5

下面是使用<inset>设置了图片的边距:

<inset xmlns:android="http://schemas.android.com/apk/res/android"
    android:drawable="@drawable/onboarding_slide4"
    android:insetLeft="30dp"
    android:insetRight="30dp"
    android:insetTop="50dp"
    android:insetBottom="50dp" >
</inset>
 
 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7

这里写图片描述 这里写图片描述


原文链接:https://liuzhichao.com/2016/android-insetdrawable.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值