android - 如何在 Android 线性布局中拉伸(stretch)/缩放背景图像?

标签 android eclipse layout background stretch

我需要一些帮助,使背景图像在线性布局中垂直和水平拉伸(stretch)或缩放。但是,每次我尝试设置背景时,它都不会拉伸(stretch)图像以适合屏幕。我也不关心保持纵横比。

这是我目前在测试 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>

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

enter image description here

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

enter image description here 我确定该解决方案非常基本,但出于某种原因我错过了它。任何帮助将不胜感激

谢谢

最佳答案

不确定布局的背景属性有哪些可用的背景布局选项,但您始终可以将 ImageView 作为具有最大宽度和高度的外部 FrameLayout 中的第一个元素。然后,您放入布局中的任何其他内容都将绘制在此图像之上。当然,您可以只加载您想要的比例类型;例如 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 - 如何在 Android 线性布局中拉伸(stretch)/缩放背景图像?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19363083/

相关文章:

Android:获取旋转的 OSM 贴图以填充整个屏幕

android - 带有 minSdkVersion 9 的项目中的 UI Automator

java - Java Eclipse SDK 入门

html - 在 HTML 表单中布局的最佳方式?

java - 跳过 build.xml 的 Eclipse 验证

java - 使用工厂模式时出现 NullPointerException

html - 使用背景图像在 CSS 布局中创建阴影的问题

android - ACTION_AUDIO_BECOMING_NOISY 未被捕获

java - driver.hidekeyboard() 在 appium 中抛出错误

android - 将具有空值的字符串作为参数传递时, Volley 字符串请求错误