我的Android进阶之旅------>Android嵌入图像InsetDrawable的用法

简介: 面试题:为一个充满整个屏幕的LinearLayout布局指定背景图,是否可以让背景图不充满屏幕?请用代码描述实现过程。 解决此题,可以使用嵌入(Inset)图像资源来指定图像,然后像使用普通图像资源一样使用嵌入图像资源。

面试题:为一个充满整个屏幕的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

<?xml version="1.0" encoding="utf-8"?>
  <inset xmlns:android="http://schemas.android.com/apk/res/android"
     android:drawable="@drawable/background"
     android:insetBottom="50dp"
     android:insetLeft="50dp"
     android:insetRight="50dp"
     android:insetTop="50dp" />
其中android:drawable="@drawable/background"引用的是drawable目录下的background.jpg文件,图像如下所示:


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

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" 
    android:background="@drawable/inset"><!-- 使用inset.xml作为背景图 -->

    <Button
        android:id="@+id/buttonRingtone"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="设置来电铃声" />

    <Button
        android:id="@+id/buttonAlarm"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="设置闹钟铃声" />

    <Button
        android:id="@+id/buttonNotification"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="设置通知铃声" />

    <EditText
        android:id="@+id/text"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" >
        <!-- 当前控件处于焦点状态 -->
        <requestFocus />
    </EditText>
</LinearLayout>

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






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

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

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

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

 


相关文章
|
3月前
|
开发工具 Android开发 开发者
Android UI设计: 解释Android的Nine-Patch图像是什么,它用于什么目的?
Android UI设计: 解释Android的Nine-Patch图像是什么,它用于什么目的?
31 4
|
4月前
|
XML 前端开发 Java
Android App开发图像加工中卡片视图CardView和给图像添加装饰的讲解以及实战(附源码 简单易懂)
Android App开发图像加工中卡片视图CardView和给图像添加装饰的讲解以及实战(附源码 简单易懂)
51 0
|
4月前
|
Android开发
Android Studio入门之图像显示解析及实战(附源码 超详细必看)(包括图像视图、图像按钮、同时展示文本与图像)
Android Studio入门之图像显示解析及实战(附源码 超详细必看)(包括图像视图、图像按钮、同时展示文本与图像)
67 1
|
2月前
|
Java Android开发 数据安全/隐私保护
安卓逆向 -- IDA基本用法
安卓逆向 -- IDA基本用法
41 0
|
8月前
|
Android开发
Android 中选项菜单(Option menu)的用法
Android 中选项菜单(Option menu)的用法
77 0
|
4月前
|
XML Java Android开发
Android App开发之图像加工中给图像添加水波动态特效(附源码和演示视频 简单易懂)
Android App开发之图像加工中给图像添加水波动态特效(附源码和演示视频 简单易懂)
40 0
|
4月前
|
XML 算法 Java
Android Studio App开发之利用图像解码器ImageDecoder播放GIF动图、Webp、HEIF图片(附源码 简单实用)
Android Studio App开发之利用图像解码器ImageDecoder播放GIF动图、Webp、HEIF图片(附源码 简单实用)
106 0
|
4月前
|
XML 安全 Java
Android Studio App开发之绘制简单的动画图像(附源码,简单易懂)
Android Studio App开发之绘制简单的动画图像(附源码,简单易懂)
49 1
|
7月前
|
编解码 网络协议 Android开发
Android平台RTMP|RTSP直播播放器功能进阶探讨
很多开发者在跟我聊天的时候,经常问我,为什么一个RTMP或RTSP播放器,你们需要设计那么多的接口,真的有必要吗?带着这样的疑惑,我们今天聊聊Android平台RTMP、RTSP播放器常规功能,如软硬解码设置、实时音量调节、实时快照、实时录像、视频view翻转和旋转、画面填充模式设定、解码后YUV、RGB数据回调等:
104 0
|
8月前
|
Android开发
Android studio中的调试(Debug)按钮具体用法
Android studio中的调试(Debug)按钮具体用法
116 0