Android Drawable Resource学习(八)、InsetDrawable

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" />

参考:

  • InsetDrawable

 

实例:

1、在xml文件中定义

inset.xml:

 

 
  1. <?xml version="1.0" encoding="utf-8"?>

  2. <inset xmlns:android="http://schemas.android.com/apk/res/android"

  3. android:drawable="@drawable/image4"

  4. android:insetLeft="50dp"

  5. android:insetRight="50dp"

  6. android:insetTop="20dp"

  7. android:insetBottom="20dp">

  8.  
  9. </inset>


layout中使用:

 

 

 
  1. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"

  2. xmlns:tools="http://schemas.android.com/tools"

  3. android:layout_width="match_parent"

  4. android:layout_height="match_parent"

  5. android:orientation="vertical"

  6. android:background="@drawable/inset" >

  7.  
  8. <Button

  9. android:id="@+id/btn"

  10. android:layout_width="wrap_content"

  11. android:layout_height="wrap_content"/>

  12. </LinearLayout>

 

可以看到,它与边界之间是有间距的。就是我们在inset.xml中所定义的。


 

 

2、java代码定义:

很简单:

 

 
  1. //new InsetDrawable(drawable, insetLeft, insetTop, insetRight, insetBottom)

  2. InsetDrawable insetDrawable=new InsetDrawable(getResources().getDrawable(R.drawable.image4), 20, 30, 30, 20);

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值