Android - 如何创建尽可能通用的布局?
我在为我的应用程序创建通用布局时遇到问题。正如预期的那样,它可以在各种设备中使用,我希望它能够在每种设备上正常工作。有几种方法可以解决这个问题,但我想创建一个 xml 文件(类似于 web.config 文件),并且在应用程序的一开始我想获取设备的屏幕宽度和高度并计算每个控件的(textview、spinner) ,按钮等)属性(例如边距,填充,宽度,高度...)根据这个宽度和高度并将这些计算值保存到我的xml文件中。最后,我想从我的布局 xml 中获取这些值,这样我的布局的视觉效果将独立于设备,并且可以在每个设备上正常工作。这能实现吗?我在互联网上找不到任何类似的解决方案。谁能帮助我吗?
提前致谢。
I am having trouble in creating generic layouts for my application. As expected, it can be used in a variety of devices and I want it to work properly for each of them. There are several approaches to achieve this problem but I want to create an xml file (similar like web.config files) and at the very beginning of my application I want to take the device's screen width and height and calculate each control's (textview, spinner, button etc.) attributes (such as margin, padding,width, height...) according to this width and height and save these calculated values into my xml file. Finally I want to reach these values from my layout xmls so my layout's visual will be independent from the device and will work properly for each device. Can this be achieved? I could not find any similar solution on the internet. Can anyone help me?
Thanks in advance.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您可以使用RelativeLayout 和类似机制来完成大部分工作,而无需对值进行硬编码。两遍动态布局系统正是针对您所描述的内容而设计的。
但是,当您需要更具体时,动态资源系统可以帮助您。对于您在 res/drawable、res/layout、res/values 等中定义的所有内容,您可以通过使用正确的格式限定同级目录来定义设备方向、像素密度、屏幕尺寸甚至语言的特定实现。在不同的文件夹中提供同名的资源,系统会根据运行时环境决定使用哪个。
看看这个:
http://developer.android.com/guide/主题/资源/providing-resources.html
You can do most of this without hard coding values using RelativeLayout and similar mechanisms. The two pass dynamic layout system is made for exactly what you're describing.
However, when you need to be more specific, that's where the dynamic resource system can help you out. For everything you define in res/drawable, res/layout, res/values, etc, you can define specific implementations for device orientation, pixel densities, screen size or even language by qualifying sibling directories with the correct format. Provide a resource with the same name in different folders, and the system will decide which to use based on the runtime environment.
Give this a look:
http://developer.android.com/guide/topics/resources/providing-resources.html
我不会使用自定义测量来动态设置布局参数。 Android 专门提供了多种功能来帮助您解决此问题(包括提供多个图像资源或特定于屏幕尺寸的布局)。
我发现,您尝试使用硬编码值自定义 Android 布局的次数越多(如果您确实想设置特定参数,请始终使用 DP)。
最重要的是,您不应该尝试重新发明轮子,而应该使用 Android 已经内置的精心设计的功能来完成您想要的事情。
I would not use custom measurements to dynamically set layout parameters. Android specifically has a variety of functionality to address this for you (including supplying multiple image resources, or layouts specific to a screen size).
I have discovered that the more you try to customize the Android layout with hard-coded values (always use DP if you do want to set specific parameters).
Bottom line, you should not try to re-invent the wheel, and just use the well-designed functionality that Android has already built-in to accomplish what you want.