在我的Android项目中,我不太确定如何使背景图片填充XML中RelativeLayout根元素的全部内容,即屏幕的大小。我想确保这适用于所有纵横比,因此图像将根据需要垂直或水平剪切。有人知道如何轻松地做到这一点吗?我只看到了有关ImageViewButton的问题,但不是真正的通用View

我的XML文件当前:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/enclosing_rl"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:background="@drawable/background"
    android:fitsSystemWindows="false">
<!-- Other elements -->
</RelativeLayout>

最佳答案

除了将图像变成 9-patch 之外,我认为这是不可能的。相反,您可以做的是在RelativeLayout中添加一个ImageView作为第一个 View 。将layout_centerInParent设置为true,并将layout_width和layout_height设置为match_parent。然后将scaleType设置为centerCrop。这样可以确保图像填满整个屏幕而不会出现任何变形,但是根据屏幕尺寸/方向,图像的顶部/底部或左侧/右侧中的某些部分可能会被切除。

<ImageView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_centerInParent="true"
        android:scaleType="centerCrop"
            android:src="@drawable/background" />

RelativeLayout中的任何其他 View 都将出现在ImageView的顶部,只要它是RelativeLayout中的第一个 View (当您位于xml中时)。

07-24 09:49
查看更多