为什么要使用 xml 布局? [英] Why should we use xml layouts?

查看:29
本文介绍了为什么要使用 xml 布局?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

经过几年的 Android 开发,我花了几个月的时间想知道为什么我应该使用 xml 布局来制作我的 Android 应用程序的 UI.

After some years of Android development, i took months wondering why should i use xml layouts to make the UI of my Android App.

我了解 MVC 和实时图形界面设计的那部分.

I understand that part of MVC and real-time graphical interface design.

如果我错了,请原谅我的无知,但是,我无法设置视图的位置,例如,8% 的屏幕宽度作为边距,70% 的屏幕宽度作为宽度.

If im wrong please take me out of ignorance and forgive me but , I´m Unable to set a view´s position, exactly, 8% of screen width for margin and 70% of screen width for width, for example.

以这种方式创建 UI 并注意屏幕纵横比可确保应用在所有设备上的外观比例完全相同.

Creating the UI in that way and taking care of screen aspect ratio ensures the app will look proportionally exactly the same in all devices.

正如这篇文章总结的那样,如果我们想支持不同的屏幕,我们被告知要为我们应用程序的每个布局使用浸入单位和一堆漂亮的 xml 文件.(如果我们必须针对每个屏幕密度使其不同,为什么他们称其为密度无关?)

As this post sums it up, we are told to use dip units and a nice bunch of xml files for every layout of our app if we want to support different screens. (Why they call it density-independent if we have to make it different for each screen density?)

但是,即使这样做,正如(再次)Google Doc 所说的

But, even doing that, as (again) the Google Doc says

dp-to-pixel的比例会随着屏幕密度的变化而变化,但不一定成正比.

The ratio of dp-to-pixel will change with the screen density, but not necessarily in direct proportion.

代替不一定,他们应该说基本上从不查看数量惊人的不同设备.根据您的项目,这可能是一个问题.(或者您希望您的客户在懂中文的设备上查看您稍微分解"的 UI)

And in place of not necessarily they should say mostly never looking at the incredible number of different devices. Depending of your project, this may be a problem. (Or do you want your client looking at your "slightly-decomposed" UI on a who knows chinese device)

我可以对图像资源提出同样的要求,但这可能是另一个问题,我过去常常将最高分辨率放在 -nodpi 上,然后通过代码将其缩小到与屏幕宽度相关的所需大小,而不是完全放置 10.000 张图像决议.

I could ask the same for the image resources, but that whould be another question, i use to put the highest resolution on -nodpi then downscale by code to screen-width-dependent desired size, instead of putting 10.000 images at all resolutions.

推荐答案

以这种方式创建 UI 并注意屏幕纵横比确保应用 看起来在比例上完全相同设备.

Apple 就是这样做的,这也是为什么当您在具有漂亮视网膜显示屏的 9.7 英寸 iPad 上打开文件夹时,它只显示 9 个图标.

This is the way Apple does it, and this why when you open a folder on a 9.7 inch iPad with beautiful retina display, it only displays 9 icons.

平板电脑不仅仅是更大的手机.真的不是.此外,iOS 设备的纵横比大致相同,因此它们是如何处理纵横比的.

A tablet is not just a bigger phone. It's really not. Also, iOS devices have mostly the same aspect ratio, so that's how they took care of the aspect ratio.

但是,在 Android 上,您不能只说您注意了纵横比";问题并且这个问题神奇地消失了.最好的办法是使用 Android 最佳做法来正确处理宽高比.

On Android however, you can't just say that you "took care of the aspect ratio" problem and that this problem magically goes away. Your best bet is to use Android best practices to handle aspect ratios correctly.

话虽如此,但也不要过度.例如,您不需要使用所有 8 个密度桶.在您之前查看官方dashboard 以了解人们实际使用的设备尝试针对每种可能的情况进行优化.

That being said, don't over do it either. You don't need to use all 8 density buckets for instance. Look at the official dashboard to see what devices people are actually using, before you try to optimize for every scenario possible.

此外,您不必一直听 Google.例如,我相信 Google 会很高兴您翻译您的应用程序,以便它在全球范围内以超过 68 种不同的语言提供,但对 Google 有利的不一定对您有利.Android 实际上允许您选择要用于制作应用程序的工具.他们不会强迫您使用任何特定的.

Also, you don't have to listen to Google all the time. For instance, I am sure that Google would love that you translate your application so that it's available in more then 68+ different languages worldwide, but what's good for Google is not necessarily good for you. Android actually lets you choose the tools you want to use to make your application. They don't force you to use any particular one.

这篇关于为什么要使用 xml 布局?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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