【问题标题】:Image not stretching inside imageView android图像没有在imageView android中拉伸
【发布时间】:2016-06-15 02:19:14
【问题描述】:

我有一个布局,其中包含根相对布局和 ImageView,当我将它设置为源时,图像不适合在图像视图中,而顶部和底部填充处于预览状态但未在 xml 中定义。我想知道如何拉伸图像以完全适合 ImageView。

XML:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    android:background="#fff">
    <ImageView
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:src="@drawable/place_demo_2"
        android:id="@+id/back_ground_place"
        android:layout_alignParentBottom="true"
        android:layout_marginBottom="400dp" />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textAppearance="?android:attr/textAppearanceLarge"
        android:text="Manila Main"
        android:id="@+id/visit_place_title"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="68dp"
        android:textColor="#fff"/>
</RelativeLayout>

【问题讨论】:

  • 设置android:scaleType="fitXY"ImageView

标签: android imageview


【解决方案1】:

使用代码:

YourImageView.SetScaleType(ImageView.ScaleType.FIT_XY);

使用xml:

android:scaleType="fitXY"

另请参阅下面的快照以了解其他替代选项。

【讨论】:

    【解决方案2】:

    在您的ImageView 中使用android:scaleType

    android:scaleType="fitXY"
    

    检查 scaleType here 的其他可用选项

    【讨论】:

      【解决方案3】:

      要么将 Imageview 的宽度设置为 wrap_content 或者 你必须将 Imageview 的 android:scaleType 设置为 fitXY

      【讨论】:

        【解决方案4】:

        添加到 ImageView 属性 android:scaleType="fitXY"

        【讨论】:

          【解决方案5】:

          您将要在 XML 文件中使用 android:scaleType。在获得您正在寻找的确切产品之前,您可能需要使用几个选项,但是,如果您不关心纵横比,听起来您可能想要使用 android:scaleType="fitXY"。

          您可以在此处找到更多信息: https://developer.android.com/reference/android/widget/ImageView.ScaleType.html

          以下是其余选项:

          ImageView.ScaleType="CENTER"

          • 在视图中居中图像,但不执行缩放。

          ImageView.ScaleType="CENTER_CROP"

          • 均匀缩放图像(保持图像的纵横比),使图像的两个尺寸(宽度和高度)都等于 或大于视图的相应尺寸(减去 填充)。

          ImageView.ScaleType="CENTER_INSIDE"

          • 均匀缩放图像(保持图像的纵横比),使图像的两个尺寸(宽度和高度)都等于 或小于视图的相应尺寸(减去填充)。

          ImageView.ScaleType="FIT_CENTER"

          • 使用 CENTER 缩放图像。

          ImageView.ScaleType="FIT_END"

          • 使用 END 缩放图像。

          ImageView.ScaleType="FIT_START"

          • 使用 START 缩放图像。

          ImageView.ScaleType="FIT_XY"

          • 使用填充缩放图像。

          ImageView.ScaleType="MATRIX"

          • 绘图时使用图像矩阵进行缩放。

          【讨论】:

            【解决方案6】:

            那是因为您已将 ImageView 与父级的底部对齐,但在 400dp 的底部有一个边距。
            长话短说,删除 android:layout_alignParentBottom="true" android:layout_marginBottom="400dp"
            如果您希望图像填满整个屏幕,还可以设置layout_height to match_parent
            P.S. 另外fill_parent 已被弃用,因此请改用match_parent

            【讨论】:

            • 不,我不想在整个屏幕上显示它,而只是附加在布局的顶部
            • 好的,没问题,删除android:layout_alignParentBottom="true" android:layout_marginBottom="400dp"
            • 我这样做了,甚至尝试了
            • 但 ImageView 内部仍有一些填充
            • 我相信边距是因为您的TextView中的android:layout_marginTop="68dp"。在你的文本视图中使用 layout_paddingTop 代替。
            猜你喜欢
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2011-06-07
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            相关资源
            最近更新 更多