Android背景图像适合屏幕没有拉伸

Son*_*tta 3 android background

我开发了一个Android应用程序.我使用android:background属性将图像作为背景.但是当它在方形显示器上显示时,图像会保持伸展状态.如何使Android背景适合屏幕而不拉伸?我见过许多应用程序的图像或视频都没有拉伸,但填满了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>
Run Code Online (Sandbox Code Playgroud)

Nei*_*eil 7

您可以尝试使用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>
Run Code Online (Sandbox Code Playgroud)

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

另请参阅主题以获取可能更符合您要求的其他解决方案.