【发布时间】: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 允许我正确裁剪它(保留整个高度并只剪掉右侧部分)。
这可以通过根据每个图像的纵横比设置每个ImageView 的layout_width 来解决。此外,将权重设置为等于图像的宽度以获得更好的分布。最右边的 LinearLayout 获取宽度 wrap_content,以便显示完整的图像。
但是,如果没有使用所有水平空间,此解决方案不会左对齐图像!
【问题讨论】:
-
所有 ImageView 的大小都一样吗?例如:所有的 ImageView 总是 100dp x 100dp?
-
我可以将它们裁剪成相等的正方形,是的。不是首选,但可以接受!
-
显示您的一些代码以便我们为您提供帮助
-
我认为我的任何非工作代码都无济于事。我宁愿需要一些提示从哪里开始。或者也许有人足够了解 FlowLayout 能够帮助我修改它..?我只是猜测,不知道从哪里开始,真的......
标签: android android-layout layout