Android_嵌入图像InsetDrawable的用法

面试题:为一个充满整个屏幕的LinearLayout布局指定背景图,是否可以让背景图不充满屏幕?请用代码描述实现过程。

解决此题,可以使用嵌入(Inset)图像资源来指定图像,然后像使用普通图像资源一样使用嵌入图像资源。

语法如下:

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


 元素解释: 
  

android:insetTop:图像距离上边的距离。

android:insetRight:图像距离右侧的距离。

android:insetBottom:图像距离底边的距离。

android:insetLeft:图像距离左侧的距离。


下面使用具体的实例来看具体的效果

首先定义了一个嵌入图像资源,res/drawable/inset.xml

[html]  view plain copy
  1. <?xml version="1.0" encoding="utf-8"?>  
  2.   <inset xmlns:android="http://schemas.android.com/apk/res/android"  
  3.      android:drawable="@drawable/background"  
  4.      android:insetBottom="50dp"  
  5.      android:insetLeft="50dp"  
  6.      android:insetRight="50dp"  
  7.      android:insetTop="50dp" />  
其中android:drawable="@drawable/background"引用的是drawable目录下的background.jpg文件,图像如下所示:

Android_嵌入图像InsetDrawable的用法_第1张图片

然后直接将inset.xml文件当做普通图像资源使用即可,代码如下:

[html]  view plain copy
  1. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  2.     android:layout_width="match_parent"  
  3.     android:layout_height="match_parent"  
  4.     android:orientation="vertical"   
  5.     android:background="@drawable/inset"><!-- 使用inset.xml作为背景图 -->  
  6.   
  7.     <Button  
  8.         android:id="@+id/buttonRingtone"  
  9.         android:layout_width="wrap_content"  
  10.         android:layout_height="wrap_content"  
  11.         android:text="设置来电铃声" />  
  12.   
  13.     <Button  
  14.         android:id="@+id/buttonAlarm"  
  15.         android:layout_width="wrap_content"  
  16.         android:layout_height="wrap_content"  
  17.         android:text="设置闹钟铃声" />  
  18.   
  19.     <Button  
  20.         android:id="@+id/buttonNotification"  
  21.         android:layout_width="wrap_content"  
  22.         android:layout_height="wrap_content"  
  23.         android:text="设置通知铃声" />  
  24.   
  25.     <EditText  
  26.         android:id="@+id/text"  
  27.         android:layout_width="match_parent"  
  28.         android:layout_height="wrap_content" >  
  29.         <!-- 当前控件处于焦点状态 -->  
  30.         <requestFocus />  
  31.     </EditText>  
  32. </LinearLayout>  

下面是具体的效果图,可以看到背景图没有占满全屏幕:

Android_嵌入图像InsetDrawable的用法_第2张图片


==================================================================================================

  作者:欧阳鹏  欢迎转载,与人分享是进步的源泉!

  转载请保留原文地址:http://blog.csdn.net/ouyang_peng

==================================================================================================  

你可能感兴趣的:(Android_嵌入图像InsetDrawable的用法)