【问题标题】:Putting ImageView on the bottom of the screen将 ImageView 放在屏幕底部
【发布时间】:2013-10-13 05:50:35
【问题描述】:

我正在尝试使用以下 XML 将 ImageView 放在布局的底部:

        <?xml version="1.0" encoding="utf-8"?>
        <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
            xmlns:tools="http://schemas.android.com/tools"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:orientation="vertical"
            tools:context=".MainActivity" >
            <LinearLayout
                android:layout_width="fill_parent"
                android:layout_height="fill_parent"
                android:background="#EEEEEE"
                android:gravity="center"
                android:orientation="horizontal" >
                <ImageView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_gravity="bottom"
                    android:src="@drawable/photo" />
            </LinearLayout>
        </LinearLayout>

我正在按照http://sandipchitale.blogspot.co.uk/2010/05/linearlayout-gravity-and-layoutgravity.html此处的说明进行操作

唯一的区别是我在这里使用 ImageView 而不是 Button:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >
    <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:gravity="center"
        android:orientation="horizontal" >
        <Button
            android:id="@+id/Button03"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="bottom"
            android:text="bottom" >
        </Button>
    </LinearLayout>
</LinearLayout>

将按钮放在预期的位置:

不幸的是,我在文章顶部的 XML 中并非如此。 ImageView 不像参考 XML 中的 Button 那样放在底部。

你知道为什么会这样吗?

【问题讨论】:

    标签: android android-layout


    【解决方案1】:

    您应该更改 ImageView 本身的设置,准确地说是在您的 ImageView 上设置 android:scaleType="fitEnd"

    其他答案似乎是解决方法,包括您自己的答案。这不是布局的问题,而是当图像的尺寸和屏幕的尺寸不相似时(即宽度/高度比非常不同),图像在 ImageView 内部的哪个位置加载。默认情况下,它将在 ImageView 的中间加载图像。

    【讨论】:

    • 这是最好的答案。谢谢你。来自 Android 文档“计算将保持原始 src 纵横比的比例,但也将确保 src 完全适合 dst。至少一个轴(X 或 Y)将完全适合。END 将结果与右边缘和下边缘对齐dst。" here
    【解决方案2】:

    尝试使用RelativeLayout而不是LinearLayout并使用参数android:layout_alignParentBottom = "true"

    编辑:尝试将 layout_height 和 layout_width 作为包含 ImageView 的 LinearLayout 的 wrap_content。

    或者可能是您使用的图像在其上方和下方有多余的空间。

    更新:

    问题在于图像的大小。它比屏幕尺寸大得多。我建议您使用以下代码以编程方式读取设备的屏幕尺寸:

    DisplayMetrics displaymetrics = new DisplayMetrics();
    getWindowManager().getDefaultDisplay().getMetrics(displaymetrics);
    int height = displaymetrics.heightPixels;
    int width = displaymetrics.widthPixels;
    

    并以编程方式设置 imageView 的高度和宽度,如下所示:

    image_view.getLayoutParams().height = height - 50;
    image_view.getLayoutParams().width = width - 50;
    

    【讨论】:

    • fill_parent 更改为 wrap_content 没有帮助。
    • 图片周围没有多余的空间。
    • 好的,您可以粘贴您正在使用的图片吗?我会尝试自己检查代码。
    • 这是图片link
    • 我绝对希望有一个可以接受宽度大于设备屏幕的图片的解决方案。
    【解决方案3】:

    在相对布局中试试这个

    <ImageView
        android:id="@+id/imageView9"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true"
        android:adjustViewBounds="?attr/isLightTheme"
        app:srcCompat="@drawable/client_app_footer" />
    

    经过测试并按预期工作

    【讨论】:

      【解决方案4】:

      您的图像视图中没有包含 android:layout_gravity="bottom" 行。

      【讨论】:

      • 一开始忘记粘贴了。我已经更新了问题 - 添加了 layout-gravity="botttom" 行。但是效果还是一样的。图片不在显示屏底部。
      • 你可以使用RelativeLayout代替Linear,然后使用android:layout_alignParentBottom="true"来对齐底部的图像。
      【解决方案5】:

      我已将照片大小从宽度 2560 调整为宽度 270,最后 ImageView 降到了底部。

      【讨论】:

        猜你喜欢
        • 2016-03-27
        • 1970-01-01
        • 1970-01-01
        • 2013-01-24
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2012-01-22
        • 2013-12-15
        相关资源
        最近更新 更多