我开发了一个安卓应用程序。我使用 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/