Android Drawable - Inset Drawable使用详解(附图)

来源:互联网 发布:人工智能的产品有哪些 编辑:程序博客网 时间:2024/06/05 10:53

一个可以让Drawable内嵌到自己View当中, 并且可以设置Drawable到View边缘之间的边距, 当然LayerDrawable也可以办到

资源放置位置:
  Eclipse/AS: res/drawable/filename.xml

引用用法:
  In Java: R.drawable.filename
  In XML: @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;
  • android:drawable: Drawable资源引用;
  • android:insetTop: Drawable距离View顶部边缘的距离;
  • android:insetRight: Drawable距离View右边边缘的距离;
  • android:insetBottom: Drawable距离View底部边缘的距离;
  • android:insetLeft: Drawable距离View左边边缘的距离;

example:
这里写图片描述

布局XML代码:

<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"              android:layout_width="match_parent"              android:layout_height="match_parent"              android:orientation="vertical">    <!--Inset Drawable-->    <TextView        android:layout_width="match_parent"        android:layout_height="match_parent"        android:background="@drawable/inset_drawable"        android:gravity="center"        android:text="Inset Drawable"        android:textAllCaps="false"/></LinearLayout>

inset_drawable.xml

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

img.xml(shape图片)

<?xml version="1.0" encoding="utf-8"?><shape xmlns:android="http://schemas.android.com/apk/res/android"      android:shape="rectangle">    <solid android:color="#00ffff"/></shape>
0 0