【问题标题】:How to stuff multiple imageViews into a single row (allow overlapping)?如何将多个 imageViews 填充到一行中(允许重叠)?
【发布时间】:2018-04-21 09:25:26
【问题描述】:

我有一定数量的缩略图图像,我想以左对齐布局,但如果图像不适合屏幕,我希望每个图像都与其左邻重叠。

它看起来像封面流或轮播视图,但是是静态的。

在一行中显示的图像数量从一开始就知道并且不会改变。另外:不需要 3D 效果。只是平坦、水平填充、重叠的图像。

我尝试使用负边距的 LinearLayout:无法确定要设置多大的边距。

我试过 PercentageRelativeLayout: 无法实现左对齐行为

请告诉我如何以编程方式进行。

我想要的一个糟糕的草图:

我实现了 Chris Parkers 的建议(虽然我必须以编程方式进行,但我使用 axml 进行测试):

<LinearLayout
  android:id="@+id/loPreviews"
  android:layout_width="match_parent"
  android:layout_height="wrap_content">
  <LinearLayout
      android:layout_width="0dp"
      android:layout_height="match_parent"
      android:layout_weight="1">
      <ImageView
          android:layout_width="@dimen/filePreviewHeight"
          android:layout_height="@dimen/filePreviewHeight"
          android:src="@drawable/img1"
          android:adjustViewBounds="true" />
  </LinearLayout>
  <LinearLayout
      android:layout_width="0dp"
      android:layout_height="match_parent"
      android:layout_weight="1">
      <ImageView
          android:layout_width="@dimen/filePreviewHeight"
          android:layout_height="@dimen/filePreviewHeight"
          android:src="@drawable/img2"
          android:adjustViewBounds="true" />
  </LinearLayout>
  <!-- more images-->
</LinearLayout>

此解决方案的问题在于,图像具有不同的纵横比,并且它们被错误地裁剪或适合带边框的正方形。不,android:scaleType 允许我正确裁剪它(保留整个高度并只剪掉右侧部分)。

这可以通过根据每个图像的纵横比设置每个ImageViewlayout_width 来解决。此外,将权重设置为等于图像的宽度以获得更好的分布。最右边的 LinearLayout 获取宽度 wrap_content,以便显示完整的图像。

但是,如果没有使用所有水平空间,此解决方案不会左对齐图像!

【问题讨论】:

  • 所有 ImageView 的大小都一样吗?例如:所有的 ImageView 总是 100dp x 100dp?
  • 我可以将它们裁剪成相等的正方形,是的。不是首选,但可以接受!
  • 显示您的一些代码以便我们为您提供帮助
  • 我认为我的任何非工作代码都无济于事。我宁愿需要一些提示从哪里开始。或者也许有人足够了解 FlowLayout 能够帮助我修改它..?我只是猜测,不知道从哪里开始,真的......

标签: android android-layout layout


【解决方案1】:

您可以使用 LinearLayout 作为父布局。然后将每个图像包装在它自己的 LinearLayout 中。每个图像容器都应该定义一个权重值,并且它们都应该具有相同的值,以便您的父布局中的空间被平均分成每个图像容器的相同数量。您的图像需要声明宽度和高度,以防止 ImageView 调整图像大小以适应容器布局。这是一个非常丑陋的解决方案,但我猜它有效。

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="100dp">

    <!-- Repeat this for every image -->
    <LinearLayout
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_weight="1">

        <ImageView
            android:layout_width="100dp"
            android:layout_height="100dp"
            android:adjustViewBounds="true"
            android:src="@drawable/image1"/>

    </LinearLayout>

    <!-- Repeat this for every image -->
    <LinearLayout
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_weight="1">

        <ImageView
            android:layout_width="100dp"
            android:layout_height="100dp"
            android:adjustViewBounds="true"
            android:src="@drawable/image2"/>

    </LinearLayout>

    <!-- ... -->
</LinearLayout>

【讨论】:

  • 我更新了我的问题,说明了我的剩余问题.. 可能你还有什么想法吗?
猜你喜欢
  • 2012-06-17
  • 2017-01-19
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-01-29
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多