微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

如何在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>   
...

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐