Android开发最佳实践
我是 Android 开发新手,我有一个关于方向更改布局的最佳实践问题。
我的问题是关于当用户更改设备方向时处理布局属性。
如果出于应用程序的目的,当方向更改时需要在布局上重新定位控件,是否应该为该特定方向创建新布局?或者是否应该尽一切努力以编程方式在单个布局上重新定位控件?
感谢您的投入。
I'm new to Android development and I have a best practices question regarding layouts on orientation change.
My question is in regards to handling layout properties when the user changes the orientation of the device.
If, for the purpose of an app, it is necessary for controls to be repositioned on the layout when the orientation is changed, should a new layout be created for that specific orientation? Or should every effort be made to reposition controls on a single layout programmatically?
Thank you for you input.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我将继续在这里不同意 thinksteep :) 我认为如果您需要超越简单的尺寸更改(例如相对于彼此移动视图),新的布局确实是最好的选择。
Android API 旨在自动处理该问题 - 您可以有一个名为
layout-land
和layout-port
的目录,分别具有单独的横向和纵向布局,系统将使用最合适的。请注意,如果可以的话,您应该避免使用单独的布局 - 维护和测试它们的工作量是您的两倍。通常,使用巧妙的权重和适当的相对布局可以使布局适用于任何分辨率。
但在某些情况下,您确实希望完全根据方向重新排列布局。举个随机的例子,看看 Google Music 的当前版本。在纵向中,屏幕的大部分是封面艺术,底部有一些信息。在横向中,左半部分是封面艺术,右半部分是信息。
最后,我还将提供 Android 文档的链接 - 查看一下。它更多的是关于多种屏幕尺寸,但它也涵盖横向/纵向。
I'll go ahead and disagree with thinksteep here :) I think a new layout is indeed the best option if you need to go beyond a simple size change (like moving views in relation to each other).
The Android API is designed to handle that automatically - you can have a directory called
layout-land
andlayout-port
with separate layouts for landscape and portrait respectively, and the system will use whichever is the most appropriate.Note that you should avoid having separate layouts if you can - it's twice as much work for you to maintain and test them. Usually, using clever weights and proper RelativeLayouts can make a layout work with any resolution.
But in some cases, you really want to rearrange a layout entirely based on the orientation. As a random example, look at the current version of Google Music. In portrait, the majority of the screen is the cover art, with some information at the bottom. In landscape, the left half is the cover art, the right half is information.
I'll also close with a link to the Android docs - check this out. It's more about multiple screen sizes, but it does cover landscape/portrait as well.
我认为新的布局不是最好的选择,您的程序应该以最佳方式重新定位控件。我建议阅读这些链接。
I think new layout is not best option, your program should reposition controls in best possible way. I would suggest reading these links.