【问题标题】:Resize android ImageView for landscape and portrait为横向和纵向调整 android ImageView
【发布时间】:2015-08-03 13:12:37
【问题描述】:

我正在尝试弄清楚如何在 android ImageView 中加载纵向或横向图像,而无需将图像拉伸到全屏。

例如,以纵向查看的横向图像不应拉伸到全屏,而是在顶部和底部居中放置黑色条纹以补偿空白空间。

有什么想法吗?

谢谢

【问题讨论】:

  • 在 res 文件夹中使用 layout-port 和 layout-land 文件夹。

标签: android android-layout


【解决方案1】:

将 imageview 的高度和宽度设置为 match_parent ,不使用任何 scaleType

    <ImageView
        android:id="@+id/img_1"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
       />

它会自动适应纵向和横向

【讨论】:

    【解决方案2】:

    要实现该效果,您需要使用scaleType 属性和黑色背景。

    <ImageView
        android:id="@+id/your_image"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:scaleType="centerInside"
        android:background="#000000"/>
    

    然后你需要设置图片资源。

    通过 xml:

    android:src="@drawable/your_image_drawable"
    

    通过代码:

    yourImageViewReference.setImageResource(R.drawable.your_image_drawable);
    

    【讨论】:

      【解决方案3】:

      在我设置的 XML 中 android:scaleType="fitXY" 现在我的纵向和横向图像都占用了相同的空间。

      【讨论】:

        猜你喜欢
        • 2015-09-09
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2011-08-29
        • 2014-10-22
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多