Android 重构 - 最佳实践 - 可绘制形状和颜色.xml

发布于 2024-11-09 14:14:04 字数 1092 浏览 0 评论 0原文

这听起来可能很愚蠢,但我能想到支持和反对的理由。

我应该将两种颜色的drawable/shape.xml 渐变重构为colors.xml 吗?

<?xml version="1.0" encoding="utf-8"?>

<shape xmlns:android="http://schemas.android.com/apk/res/android">
    <gradient 
        android:startColor="#FFFFFF" 
        android:endColor="#CCCCCC"
        android:angle="270" />
    <stroke 
        android:width="3dp" 
        color="@color/transparent" />
    <corners 
        android:radius="13dp" />
    <padding 
        android:left="10dp" 
        android:top="10dp"
        android:right="10dp" 
        android:bottom="10dp" />
</shape>

将其保存在drawable/shape.xml 中表明它是一个实体(一个按钮)并允许它安全地保存在一个地方。 将其移至colors.xml 允许这些颜色与其他资源共享,也许如果我正在创建主题或样式?但是当查看 color.xml 时,我怎么知道它们组合起来形成渐变颜色?

有没有人有意见,我希望我已经添加了足够的思考过程来引发一些辩论。

我尝试查看 IO Sched 应用程序的linky 查看 Google 员工做了什么,但我找不到他们创建任何形状。

This may sound stupid, but I can think of reasons for and against.

Should I refactor the two color's out my drawable/shape.xml gradient into colors.xml?

<?xml version="1.0" encoding="utf-8"?>

<shape xmlns:android="http://schemas.android.com/apk/res/android">
    <gradient 
        android:startColor="#FFFFFF" 
        android:endColor="#CCCCCC"
        android:angle="270" />
    <stroke 
        android:width="3dp" 
        color="@color/transparent" />
    <corners 
        android:radius="13dp" />
    <padding 
        android:left="10dp" 
        android:top="10dp"
        android:right="10dp" 
        android:bottom="10dp" />
</shape>

Keeping it in drawable/shape.xml show's that it is one entity ( a button ) and allows it to stay safe in one place.
Moving it to colors.xml allows these color's to be shared with other resource perhaps if I was creating a theme or style? But then when looking at colors.xml how would I know they combine to make a gradient color?

Does anyone have an opinion, I hope I've added enough of my though process to spark some debate.

I tried looking at the Source for the IO Sched app: linky to see what Google Employee's did, but I can't find them creating any shapes.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

凉栀 2024-11-16 14:14:04

我想说,一般来说,您应该提取颜色资源,但请确保按逻辑命名它们。

例如,如果您有类似 accent_color_1accent_color_1_darker 的内容,则应假设应用中使用的这两种颜色之间可能存在色带。然而,如果您从 accent_color_1accent_color_2 等颜色渐变,那就没有什么意义了。

但归根结底,这实际上只是个人或团队偏好的问题。

I'd say that in general you should extract color resources, but make sure to name them logically.

For example, if you have something like accent_color_1 and accent_color_1_darker, one should assume there could be a color ramp between these two colors used in the app. It would make less sense, however, for you to color ramp from something like accent_color_1 and accent_color_2.

At the end of the day, though, it's really just a matter of personal or team preference.

失眠症患者 2024-11-16 14:14:04

我更喜欢在单独的 xml 文件中定义所有颜色。

除了能够在其他资源中使用它们之外,您还可以为每种颜色指定一个描述其用途的名称。因此,当您查看形状代码时,您将看到一些有意义的内容,而不是一些神秘的十六进制字符。

如果你能确保只在一个地方使用特定的颜色,我想这只是一个品味问题。

I prefer defining all the colors in the separate xml file.

Apart from being able to use them in other resources you can give each color a name that describes what is it used for. So when you look at your shape code you will be reading something that makes sense and not some arcane hexadecimal characters.

If you can ensure that you are going to use that specific color just in one place, I guess it's just a matter of taste.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文