简体   繁体   English

Android布局设计:layout.xml与dimens.xml

[英]Android layout design: layout.xml vs dimens.xml

I am a newbie android developer. 我是新手android开发人员。 Recently I am reading Google's specification of providing alternative resources to support different screen size. 最近,我正在阅读Google关于提供其他资源以支持不同屏幕尺寸的规范。

https://developer.android.com/training/multiscreen/screensizes.html https://developer.android.com/training/multiscreen/screensizes.html

I know we can provide resource directories with specific qualifiers to achieve this, 我知道我们可以为资源目录提供特定的限定词来实现这一目标,

eg layout-sw320dp/layout.xml , layout-sw480dp/layout.xml , layout-sw600dp/layout.xml . 例如layout-sw320dp/layout.xmllayout-sw480dp/layout.xmllayout-sw600dp/layout.xml

Meanwhile, I also found some developers who create various dimens.xml in the values folders with different screen size qualifiers, 同时,我还发现一些开发人员在具有不同屏幕尺寸限定符的values文件夹中创建各种dimens.xml

eg values-sw320dp/dimens.xml , values-sw480dp/dimens.xml , values-sw600dp/dimens.xml . 例如values-sw320dp/dimens.xmlvalues-sw480dp/dimens.xmlvalues-sw600dp/dimens.xml

I am wondering which approach would be better for maintaining the layout. 我想知道哪种方法更适合维护布局。 My intuition told me if I have controlled the screen size rendering by different layout.xml . 我的直觉告诉我,是否通过其他layout.xml控制了屏幕尺寸的渲染。 Then I do not need to use dimens.xml to fit different screen size. 然后,我不需要使用dimens.xml来适应不同的屏幕尺寸。 Is it a right thought? 这是正确的想法吗?

You should use dimens.xml if the only thing changing is the size. 如果唯一改变的是大小,则应使用dimens.xml。 You should use layout.xml if you're changing the actual views. 如果要更改实际视图,则应使用layout.xml。

Why? 为什么? Because of maintenance. 由于维护。 If in 3 months you decide you want to change the background color of a view and you have 4 layout files, you need to go and change it in 4 places. 如果在3个月内您决定要更改视图的背景色,并且有4个布局文件,则需要在4个位置进行更改。 Eventually you'll forget one. 最终,您会忘记一个。 If you used dimens properly, you only need to change it in 1 place. 如果正确使用了调光,则只需要在1个位置进行更改即可。

Prefer to use dimens wherever possible. 尽可能使用调光器。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM