Android 背景图片适配屏幕无拉伸(stretch)

标签 android background

我开发了一个安卓应用程序。我使用 android:background 属性将图像作为背景。但是当它显示在方形显示器上时,图像会一直拉伸(stretch)。 如何让 Android Background 不拉伸(stretch)地适合屏幕? 我见过许多应用程序的图像或视频未拉伸(stretch),但会填满 Android 手机的整个屏幕。

我的xml代码如下

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
    android:layout_height="match_parent" tools:context=".MainActivity"
    android:background="@drawable/Splash" >

</RelativeLayout>

最佳答案

您可以尝试使用 ImageView 而不是将其设置为 RelativeLayout 的背景

我已经修改了你的代码,看起来像这样

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
    android:layout_height="match_parent" tools:context=".MainActivity">

    <ImageView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:scaleType="centerCrop"
        android:src="@drawable/Splash"/>
</RelativeLayout>

此解决方案的唯一问题是图像在某些设备上可能会被部分截断。为了解决这个问题,您可以为相关设备创建一些具有不同宽高比的图像并将它们放入相关的drawable-xxxx文件夹中。

另请查看 this其他可能更适合您要求的解决方案的线程。

关于Android 背景图片适配屏幕无拉伸(stretch),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28847652/

相关文章:

android - 我的android应用程序在我的模拟器上运行良好,但是在设备上崩溃了

java - 由 : java. lang.ClassNotFoundException 引起:在 Firebase 电话身份验证中找不到类

android - 我们可以为标签设置背景图片吗?

ios - 后台运行 iOS App 超过 10 分钟

css - CSS 中带有圆 Angular 三 Angular 形/箭头的容器

html - 线性渐变作为主体背景

python - Pygame 播放列表在后台连续播放

java - list 合并因多个错误而失败,请参阅日志中的一个信号

android - Crashlytics 的仪表板打不开

android - 通过 phonegap videoplayer-plugin 从 res/raw 文件夹播放 mp4 视频