如何将图像向上缩放并将其居中放置在容器的底部?

时间:2014-11-20 20:05:58

标签: android android-layout imageview scale centering

我正在尝试使用以下属性制作一个非常简单的布局:

  • 只包含一个应该是的图片:
    • 在保持原始宽高比的同时尽量放大
    • 水平居中
    • 在其容器的底部

这是一个应该是什么样子的例子:

portrait view showing image centred at the bottom but scaled up landscape view showing image centred at the bottom but scaled up

通过将图片设置为match_parent宽度和高度并指定scaleType fitCentre,我可以将其放大并水平和垂直居中。我还可以使用fitEnd将其与右下角对齐。

我尝试使用RelativeLayout将其居中放在底部,但如果我将图片的宽度和高度设置为wrapContent,那么我只能将其设置为正常工作,这会导致它无法向上扩展。< / p>

以下是一个用作起点的示例布局:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >

    <ImageView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_alignParentBottom="true"
        android:layout_centerHorizontal="true"
        android:layout_gravity="bottom|center_horizontal"
        android:scaleType="fitStart"
        android:src="@drawable/abc_ic_search" />

</RelativeLayout>

1 个答案:

答案 0 :(得分:1)

试试这个:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
                android:layout_width="match_parent"
                android:layout_height="match_parent">

    <ImageView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:scaleType="fitCenter"
            android:src="@drawable/abc_ic_search"
            android:adjustViewBounds="true"
            android:layout_alignParentBottom="true"/>

</RelativeLayout>