如何在Android线性布局中拉伸/缩放背景图像?

我需要一些帮助使背景图像在线性布局中纵向和横向拉伸或缩放.然而,每次我尝试设置背景时,它都不会拉伸图像以适应屏幕.我也不关心保持纵横比.

这是我目前在测试xml中得到的:

<LinearLayout 
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/title"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:background="@drawable/background">
</LinearLayout>

在预览中(以及在游戏中)看起来像这样:

如果我将layout_height更改为fill_parent,我会得到一个重复的背景:

我确信解决方案非常基础,但由于某种原因我错过了它.任何帮助,将不胜感激

谢谢

解决方法

不确定哪些可用的背景布局选项可用于布局的背景属性,但您始终只需将ImageView作为外部FrameLayout中具有最大宽度和高度的第一个元素.然后,您在布局中放置的任何其他内容都将绘制在此图像的顶部.当然,你可以加载你想要的scaletype;比如fitXY也许就是你想要实现的目标.
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >

<ImageView 
    android:id="@+id/background"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:contentDescription="@string/backgroundDescription"
    android:scaleType="centerCrop">
</ImageView>   
...

相关文章

Android性能优化——之控件的优化 前面讲了图像的优化,接下...
前言 上一篇已经讲了如何实现textView中粗字体效果,里面主要...
最近项目重构,涉及到了数据库和文件下载,发现GreenDao这个...
WebView加载页面的两种方式 一、加载网络页面 加载网络页面,...
给APP全局设置字体主要分为两个方面来介绍 一、给原生界面设...
前言 最近UI大牛出了一版新的效果图,按照IOS的效果做的,页...