如何使图像填充RelativeLayout的背景不拉伸 [英] How to make image fill RelativeLayout background without stretching

查看:3793
本文介绍了如何使图像填充RelativeLayout的背景不拉伸的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的Andr​​oid项目,我不太知道如何让我的背景图像填充XML中的 RelativeLayout的根元素,它是屏幕尺寸的整体。我想肯定这适用于所有的纵横比,因此图像会垂直或水平夹是必要的。是否有人知道如何轻松地做到这一点?我只看过关于的ImageView 按钮 S,但没有真正的通用查看问题秒。

我的XML文件目前:

 < XML版本=1.0编码=UTF-8&GT?;
< RelativeLayout的的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android
    机器人:ID =@ + ID / enclosing_rl
    机器人:layout_width =FILL_PARENT
    机器人:layout_height =FILL_PARENT
    机器人:背景=@可绘制/背景
    机器人:fitsSystemWindows =假>
<! - 其他元素 - >
< / RelativeLayout的>
 

解决方案

除了将你的图像分成九个补丁,我不认为这是可能的。你可以做的反而是增加一个ImageView的在你的RelativeLayout的第一个观点。设置layout_centerInParent为真,并有layout_width和layout_height设置为match_parent。然后设置scaleType为centerCrop。这将确保图像填满整个屏幕没有任何失真,但根据屏幕大小/方向或者部分顶部/底部或左图像/右侧可能被截断。

 < ImageView的
        机器人:layout_width =match_parent
        机器人:layout_height =match_parent
        机器人:layout_centerInParent =真
        机器人:scaleType =centerCrop
            机器人:SRC =@可绘制/背景/>
 

在RelativeLayout的任何其它视图将显示在ImageView的顶部,只要它是在RelativeLayout的第一视图(当你在XML)。

In my Android project, I am not quite sure how to make my background image fill the entirety of the RelativeLayout root 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.

My XML file currently:

<?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>

解决方案

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.

<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的背景不拉伸的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆