Android 如何在不拉伸的情况下使图像填充RelativeLayout背景

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11275897/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-20 06:39:38  来源:igfitidea点击:

How to make image fill RelativeLayout background without stretching

androidbackgroundandroid-relativelayout

提问by pqn

In my Android project, I am not quite sure how to make my background image fill the entirety of the RelativeLayoutroot element in XML, which is the size of the screen. I want to be sure that this works for all aspect ratios, so the image will clip vertically or horizontally as necessary. Does someone know how to do this easily? I've only seen questions regarding ImageViews and Buttons, but not really generic Views.

在我的 Android 项目中,我不太确定如何让我的背景图像填充RelativeLayoutXML 中的整个根元素,即屏幕的大小。我想确保这适用于所有纵横比,因此图像将根据需要垂直或水平剪辑。有人知道如何轻松做到这一点吗?我只看到了关于ImageViews 和Buttons 的问题,但不是真正通用的Views。

My XML file currently:

我的 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>

回答by Flynn81

Other than turning your image into a nine patch I don't think this is possible. What you could do instead is add an ImageView as the first view in your RelativeLayout. Set layout_centerInParent to true and have the layout_width and layout_height set to match_parent. Then set scaleType to centerCrop. That will make sure the image fills the screen without any distortion, but depending on screen size/orientation either some of the top/bottom or left/right of the image may be cut off.

除了将您的图像变成九个补丁之外,我认为这是不可能的。您可以做的是在您的 RelativeLayout 中添加一个 ImageView 作为第一个视图。将 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" />

Any other views in the RelativeLayout will appear on top of the ImageView, as long as it is the first view in the RelativeLayout (when you are in the xml).

RelativeLayout 中的任何其他视图都会出现在 ImageView 之上,只要它是 RelativeLayout 中的第一个视图(当您在 xml 中时)。

回答by tiguchi

Create a bitmap drawable XML resource in your res/drawable folder:

在 res/drawable 文件夹中创建位图可绘制 XML 资源:

<?xml version="1.0" encoding="utf-8"?>
<bitmap xmlns:android="http://schemas.android.com/apk/res/android"
    android:src="@drawable/background"
    android:tileMode="repeat" />

Use that drawable as background instead of @drawable/background

使用该 drawable 作为背景而不是 @drawable/background

回答by Amir Hossein Ghasemi

according to this answerIf you want your ImageViewfill your RelativeLayout,use alignparameters for ImageView.

根据这个答案如果你想要你的ImageView填写你的RelativeLayout,使用align参数ImageView

you can put all of your views to a LinearLayoutand set alignparameter to your background ImageView:

您可以将所有视图放在 aLinearLayout并将align参数设置为您的背景ImageView

<RelativeLayout
    android:layout_width="fill_parent"
    android:layout_height="wrap_content" >

     <ImageView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:src="@drawable/my_background"
        android:scaleType="centerCrop"
        android:layout_alignTop="@+id/my_views"
        android:layout_alignBottom="@+id/my_views"

        />

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/my_views"
        android:orientation="vertical" >
    <!-- stuff -->
    </LinearLayout>


</RelativeLayout>

回答by Sheharyar Ejaz

Its smart and 100% working answer is to set the property scaleType of image view !

它的智能和 100% 工作答案是设置图像视图的属性 scaleType !

 android:scaleType="fitCenter"