如何在Android中实现行为类似于具有属性"float:left"的HTML块的视图? [英] How to implement a view in Android that behaves like a HTML block with property "float:left"?

查看:163
本文介绍了如何在Android中实现行为类似于具有属性"float:left"的HTML块的视图?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在此示例中,块#4和#6移至下一行,因为当前行上没有足够的空间容纳它们.它的行为与具有CSS属性(例如"display:block; float:left;")的HTML中的block元素基本相同.让我们以我要以这种方式在屏幕上放置图像的情况为例.如何实现呢?

In this example the blocks #4 and #6 are moved to the next line because there isn't enough space for them on the current line. It is basically the same behaviour as a block element in HTML with CSS properties like "display: block; float: left;". Let's take the situation where I want to place images on the screen in this fashion. How to implement this?

推荐答案

GridLayout最接近,但是它将其子级调整为相等的大小,因此对于您显示的示例图像不起作用.如果您只是在谈论图像/按钮/任何内容的一致网格,那么这是完美的选择.

GridLayout comes closest, but it resizes its children to equal sizes, so it wouldn't work for the example image you've shown. If you're just talking about a consistent grid of images/buttons/whatever, it's perfect for that.

另外,还有一个第三方解决方案: FlowLayout .

For the other, there is a third party solution: FlowLayout.

它基于LinearLayout,因此您只需将其设置为水平方向并添加所有您喜欢的元素.它将在需要的时候流入下一行,而无需您自己执行测量/调整.

It's based on a LinearLayout, so you just set it to horizontal orientation and add all the elements you like. It will flow to the next line when it should, and no need to implement measuring/adjusting on your own.

注意:我已经有一段时间没有对此进行测试了,所以我不确定它在最新的API上的表现如何.我上次使用它是针对Gingerbread应用程序,当时它运行良好.

Note: I haven't tested this in a while, so I'm not sure how it behaves on the newest APIs. The last time I used it was for a Gingerbread app, and it worked fine at that point.

这篇关于如何在Android中实现行为类似于具有属性"float:left"的HTML块的视图?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆