Why should we use xml layouts?

2019-01-01 14:02发布

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

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

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.

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.

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?)

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

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

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)

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.

1条回答
浪荡孟婆
2楼-- · 2019-01-01 14:14

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.

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.

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.

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.

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.

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.

查看更多
登录 后发表回答