如何在Android布局中包裹图像、按钮等内容?
我想在布局中放置按钮列表,并且希望它们自动流动。看看这张图片,这就是WrapPanel 将其内容包装在 WPF 中。有什么方法可以在 Android 中做到这一点吗?
(来源:microsoft.com)
I want to place list of buttons in a layout and I want them to flow to automatically. Take a look at this picture, This is how WrapPanel wraps its content in WPF. Is there a way I can do this in Android?
(source: microsoft.com)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您在 android 中使用线性布局,它允许以垂直或水平顺序布局子项。您将按钮放在线性布局内。
您应该阅读此有关 Android 布局的教程。链接中的 AbsoluteLayout 已被弃用。
我对包裹面板的了解纯粹基于您附加的链接,因为我没有使用过 WPF。
这是我的理解。
Android 没有环绕面板布局,当空间较小时,子视图会中断到下一行。您的选择是
如果视图大于屏幕,则使用滚动视图进行滚动。
对线性布局中的子视图使用layout_weight属性。这将使观点
调整自身大小以适应变化的尺寸。
通过扩展 ViewGroup 类来创建自定义布局,您可以在其中测量子视图并根据可用屏幕宽度决定它们的位置。
前两个选项并不完全是您想要的。它们是 android 提供的替代方法。但如果你想要精确的东西,你需要自己制作。
You use a linearlayout in android which allows to layout childs in either vertical or horizontal order. You put the buttons inside the linearlayout.
You should go through this tuorial on android layouts. AbsoluteLayout in the link is depracated.
My knowledge of wrap panel is purely based on the link you attached as I haven't worked with WPF.
This is from what i understood.
Android doesn't have a wrap panel layout where child views break to next line when there is less space. Your options are
to use scrollviews to scroll if the views are bigger than the screen.
Use the layout_weight property for childs views in a linearlayout. This will make the views to
resize itself to changing sizes.
Create a custom layout by extending the ViewGroup class where you measure the child views and decide their position based on the available screen width.
The first 2 options are not exactly what you want. They are alternative approaches with what android provides. But if you want exact thing, you need to make it yourself.
这看起来像一个 LinearLayout:
有关在 Android 中使用 XML 布局的更多信息,请参阅 android 开发人员网站上的这篇精彩介绍:http://developer.android.com/resources/tutorials/views/index.html
This looks like a LinearLayout:
For more info on using XML layouts in Android, see this good introduction on the android Developer site:http://developer.android.com/resources/tutorials/views/index.html
我想你需要使用RelativeLayout才能轻松实现这一点。
这是代码:
希望这有帮助! ;)
i guess you need to use RelativeLayout in order to achieve that easily.
here's the code:
Hope this helps! ;)