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
How to make image fill RelativeLayout background without stretching
提问by pqn
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 ImageView
s and Button
s, but not really generic View
s.
在我的 Android 项目中,我不太确定如何让我的背景图像填充RelativeLayout
XML 中的整个根元素,即屏幕的大小。我想确保这适用于所有纵横比,因此图像将根据需要垂直或水平剪辑。有人知道如何轻松做到这一点吗?我只看到了关于ImageView
s 和Button
s 的问题,但不是真正通用的View
s。
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 ImageView
fill your RelativeLayout
,use align
parameters for ImageView
.
根据这个答案如果你想要你的ImageView
填写你的RelativeLayout
,使用align
参数ImageView
。
you can put all of your views to a LinearLayout
and set align
parameter 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"