colors.xml_命名约定colors.xml Android

colors.xml

It is one thing to come up with intuitive naming for String keys but its totally another to manage and organise your colors in an Android project.

直观地命名String键是一回事,而在Android项目中管理和组织颜色则完全是另一回事。

The Problem :

问题 :

Your colors.xml will initially have the primary, primaryDark and accent by default, based on your theme pallette.

默认情况下,根据主题调色板,您的colors.xml最初将具有primary,primaryDark和accent。

Later you would add few colors with their proper name. ex: white, black, red etc,.

稍后,您将添加一些带有适当名称的颜色。 例如:白色,黑色,红色等。

But before you could realise, you would have started adding the location of the color, as the key, rather than the color itself. ex: login_bg_color

但是在意识到之前,您应该已经开始添加颜色的位置作为键,而不是颜色本身。 例如:login_bg_color

Image for post

This is a very common scenario in many Android projects. Developers tend to ignore these small details, but these count a lot when work on a Strong UI focused project.

在许多Android项目中,这是非常常见的情况。 开发人员倾向于忽略这些小细节,但是在以Strong UI为重点的项目上工作时,这些细节非常重要。

Image for post
Usual scenario in an Android project
Android项目中的常规场景

There are many naming conventions to follow. Android Material design suggests to use color_shade format but its not completly unique and easily referenancable. I would’nt be going deep inside good practises from Material design etc,.

遵循许多命名约定。 Android Material设计建议使用color_shade格式,但它并非完全独特且易于引用。 我不会深入研究材料设计等方面的良好实践。

Rather, I am suggesting a very simple approach.

相反,我建议一种非常简单的方法。

最佳实践: (Best Practice:)

Advantages of the solution being proposed :

建议的解决方案的优点:

  • Prevent duplications of same colors with different names

    防止名称重复相同的颜色
  • Color names will be unique and self-descriptive

    颜色名称将是唯一且具有自我描述性

Naming convention :

命名约定:

You can use websites to get the name of your color so it remains unique. Ex:

您可以使用网站获取颜色名称,以便使其保持唯一。 例如:

  1. http://chir.ag/projects/name-that-color/

    http://chir.ag/projects/name-that-c​​olor/

  2. https://www.color-blindness.com/color-name-hue/

    https://www.color-blindness.com/color-name-hue/

Image for post
Color name using online tools
使用在线工具的颜色名称

The color name produced by tools like these might be non-intuitive for most of us but that shouldn’t be an issue because ideally we would not be using all the colors everywhere inside the application.

这样的工具所产生的颜色名称对我们大多数人来说可能并不直观,但这不应该成为问题,因为理想情况下,我们不会在应用程序内部各处都使用所有颜色。

Image for post

There are scenarios where two colors might produce the same name. It would mean that you need to rethink your color usage, or create a color xml with alpha to differentiate between the two

在某些情况下,两种颜色可能会产生相同的名称。 这意味着您需要重新考虑您的颜色使用情况,或创建带有alpha的颜色xml以区分两者

If you feel the need to make your colors more intuitive, then adding the dominant color as prefix should help a bit.

如果您需要使颜色更直观,那么添加主要颜色作为前缀应该会有所帮助。

ex: shamrock -> green_shamrock

例如:三叶草-> green_shamrock

Image for post

You can use sites like https://www.color-blindness.com/color-name-hue/ to get the hue color

您可以使用https://www.color-blindness.com/color-name-hue/之类的网站来获取色相

Α: (Alpha:)

You run into situations where you colors have varied alpha. You easily get tempted to add suffix like _10, _80 to your existing colors. This is a bad approach.

您会遇到颜色阿尔法不同的情况。 您很容易想起将_10,_80之类的后缀添加到现有颜色中。 这是一个不好的方法。

Follow the advice of Nick Butcher and Chris Banes and create a SingleItem ColorStateList for similar situations.

遵循Nick ButcherChris Banes的建议,并为类似情况创建SingleItem ColorStateList。

翻译自: https://medium.com/modulotech/naming-conventions-colors-xml-android-8f89139f1056

colors.xml

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值