惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

P
Privacy & Cybersecurity Law Blog
Engineering at Meta
Engineering at Meta
Forbes - Security
Forbes - Security
MongoDB | Blog
MongoDB | Blog
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
A
About on SuperTechFans
量子位
Exploit-DB.com RSS Feed
Exploit-DB.com RSS Feed
雷峰网
雷峰网
腾讯CDC
P
Proofpoint News Feed
S
Schneier on Security
S
Secure Thoughts
V
Visual Studio Blog
Help Net Security
Help Net Security
The Hacker News
The Hacker News
C
Cyber Attacks, Cyber Crime and Cyber Security
P
Privacy International News Feed
SecWiki News
SecWiki News
S
SegmentFault 最新的问题
T
Threatpost
小众软件
小众软件
MyScale Blog
MyScale Blog
F
Fortinet All Blogs
cs.CL updates on arXiv.org
cs.CL updates on arXiv.org
P
Proofpoint News Feed
T
Tailwind CSS Blog
I
Intezer
C
CERT Recently Published Vulnerability Notes
U
Unit 42
V
V2EX
Cyberwarzone
Cyberwarzone
Recorded Future
Recorded Future
O
OpenAI News
Project Zero
Project Zero
有赞技术团队
有赞技术团队
Google DeepMind News
Google DeepMind News
Last Week in AI
Last Week in AI
Hugging Face - Blog
Hugging Face - Blog
Know Your Adversary
Know Your Adversary
C
Cybersecurity and Infrastructure Security Agency CISA
Scott Helme
Scott Helme
V2EX - 技术
V2EX - 技术
博客园 - 叶小钗
S
Securelist
A
Arctic Wolf
The Cloudflare Blog
W
WeLiveSecurity
T
Threat Research - Cisco Blogs
博客园 - Franky

DEV Community

Authentication Security Deep Dive: From Brute Force to Salted Hashing (With Java Examples) Why AI Systems Don’t Fail — They Drift Spilling beans for how i learn for exam😁"Reinforcement Learning Cheat Sheet" I Replaced Chrome with Safari for AI Browser Automation. Here's What Broke (and What Finally Worked) How Python Borrows Other People's Work The $40 Architecture: Processing 1 Billion API Requests with 99.99% Uptime Vibe Coding: A Workflow Guide (From Zero to SaaS) Most webhook security guides protect the wrong side. The scary part is delivery. Headless CMS for TanStack Start: Build a Blog with Cosmic EU Age Verification App "Hacked in 2 Minutes" — What Actually Happened Comfy Cloud’s delete function does not actually remove files Running AI Models on GPU Cloud Servers: A Beginner Guide Event-driven media intelligence with AWS Step Functions and Bedrock I scored 500 AI prompts across 8 quality dimensions — here's what broke How to Call Google Gemini API from Next.js (Free Tier, No Backend Needed) The Portal Protocol: Reclaiming Human Connection in the Age of AI How to Fix Your Team's Scattered Knowledge Problem With a Self-Hosted Forum Intro to tc Cloud Functors: A Graph-First Mental Model for the Modern Cloud Designing Multi-Tenant Backends With Both Ownership and Team Access I Built a Neumorphic CSS Library with 77+ Components — Here's What I Learned PostgreSQL Performance Optimization: Why Connection Pooling Is Critical at Scale Cómo construí un SaaS multi-rubro para gestionar expensas en Argentina con FastAPI + Vue 3 🚀 I Built an Ethical Hacking Scanner Tool – Open Source Project I Replaced /usage and /context in Claude Code With a Single Statusline A Pythonic Way to Handle Emails (IMAP/SMTP) with Auto-Discovery and AI-Ready Design I Collected 8.9 Million Polymarket Price Points — Here's What I Found About How Markets Really Move EcoTrack AI — Carbon Footprint Tracker & Dashboard Everyone's Using AI. No One Agrees How. 5 self-hosted ebook managers worth trying in 2026 Building Your First AI Agent with LangChain: From Chatbot to Autonomous Assistant Common SOC 2 Failures (Real World) Stop Vibe-Checking Your AI App: A Practical Guide to Evals How to Use SonarQube and SonarScanner Locally to Level Up Your Code Quality Your Next To-Do App Is Dead — I Replaced Mine with an OpenClaw AI Sign a Nostr event in 60 lines of Python using coincurve — no nostr-sdk, no nbxplorer, no rust toolchain ITGC Audit Explained Like You’re in Big 4 Patch Tuesday abril 2026: Microsoft parcha 163 vulnerabilidades y un zero-day en SharePoint Stop scraping everything: a better way to track competitor price changes Listing on MCPize + the Official MCP Registry while routing payments OUTSIDE the marketplace — how I kept 100% of my x402 revenue Building an AI-Powered Risk Intelligence System Using Serverless Architecture Why We Ripped Function Overloading Out of Our AI Toolchain Testing AI-Generated Code: How to Actually Know If It Works SaaS Churn Is Killing Your Business. Here Is What to Do About It (Without a Support Team) The Speed of AI Is No Longer Linear - And Self-Improving Models Are Why How to Implement RBAC for MCP Tools: A Practical Guide for Engineering Teams From Standard Quote to Persuasive Proposal: AI Automation for Arborists I built a CLI that scaffolds complete multi-tenant SaaS apps Axios CVE-2025–62718: The Silent SSRF Bug That Could Be Hiding in Your Node.js App Right Now The dashboard that ended our friendship Data Pipelines Explained Simply (and How to Build Them with Python) The Hidden Cost of AI Systems Nobody Talks About. undefined vs undeclared, and how typeof behaves Switching from file-based jobs to NATS/Kafka in Rust without changing code io_uring Adventures: Rust Servers That Love Syscalls Why Agentic AI is Killing the Traditional Database The POUR principles of web accessibility for developers and designers Quantum Neural Network 3D — A Deep Dive into Interactive WebGL Visualization How To Install Caveman In Codex On macOS And Windows Automation Pipeline Reliability: Why Your Workflow Breaks When Nobody Is Watching I Built an 'Open World' AI Coding Agent — It Works From ANY Folder From Freelancing to Product: A Tech Service Company's SaaS Transformation China's AI Giants: Adding Tencent Hunyuan & ByteDance Doubao to AI University (74 Providers) On the Vibe Coders and Their Lies clerk: Auto-Summarize Your Claude Code Sessions AI Weekly — 2026/04/10–04/17 | The Model Lockdown Is Here, but the Toolchain Is the Real Battleground AI 週報 — 2026/04/10–2026/04/17 模型封鎖潮來了,但工具鏈才是真戰場 Maybe this is how Open-Source apps are born... 🚀 Fine-Tune LLMs with LoRA and QLoRA: 2026 Guide tRPC v11 + Next.js App Router: End-to-End Type Safety Without the Boilerplate ShadCN UI in 2026: Why I Stopped Installing Component Libraries and Started Owning My Components SaaS Billing in React Server Components: Stripe + Supabase Without a Single `useEffect` Join our DEV Weekend Challenge — $1,000 in Prizes Across TEN winners! Submissions Due April 20 at 6:59 AM UTC. Implementing FSRS Spaced Repetition in Flutter + Supabase — Adding Memory Science to an AI Learning App "I Texted My Localhost From the Train — Claude Code Fixed the Bug Before I Got Home" I Built a Sales Prep AI and It Went Deeper Than Expected Design to Code #2: One JSON, Eleven Outputs Solving the 100M-Row Problem: A Summary Table Pattern for High-Volume Push Notification Logs Flutter Web With Wasm: What Actually Changes For Developers I Built 50 Royalty-Free Soundtracks for My Side Project in a Weekend Using AI Music Generation The Vibe Coding Security Checklist: 7 Things to Check Before You Ship Stop Letting Googlebot Guess Fix Your React App's SEO Right Desconstruindo o Streaming do LinkedIn: Como Criar um Engine de Extração de Vídeo de Alta Performance com HLS e FFmpeg (EDA Part-1) EDA (Exploratory Data Analysis) Explained With Real Life — Why Looking at Your Data Is the Most Important Step in Machine Learning Brand Relationship Management at Scale: Our 4-Touch Outreach System for 200+ Brands Why String.fromEnvironment() Might Return an Empty String in Dart JGuardrails 1.0.0 — Hardening Java LLM Apps Against Jailbreaks, Toxicity, and Prompt Injection Plan and Schedule a Full Week of Threads Content From One Claude Conversation Coding Cat Oran Ep3, Five Tables Changed Everything Updated: BFF Pattern I'm done watching freelancers get buried by 200 proposals. So I'm building the alternative. This is my first post BFS Algorithm in Java Step by Step Tutorial with Examples Tracking LLM Pricing Monthly: An Open Dataset for 22 AI Models How We Measure Content ROI on a Comparison Site: Revenue Attribution Without Perfect Data Introducing Nova AI Ops: The AI-Native Operating System for SRE Teams I built a free desktop video downloader for Windows — Grabbit How Talkie OCR Helps Vision-Impaired & Dyslexic Users Read the World Around Them VRCFaceTracking安装和iPhone面捕配置教程,有bug Even CrowdStrike Can't See Your Agents The Automation Gold Rush: What n8n Workflows and Claude Are Opening Up for Developers Right Now
现代化 UI 架构:Drawable 层规范与工程实践
goodlords · 2026-05-19 · via DEV Community

goodlords

本文是 UI 架构系列的第二篇,建议先阅读 第一篇:三层颜色体系与系统化设计方案 了解核心设计理念。


前言

在上一篇文章中,我们建立了一套完整的颜色体系。但颜色只是 UI 的基础,真正让 UI 活起来的是形态和交互

一个按钮不仅需要颜色,还需要:

  • 圆角、描边、阴影
  • 按下、禁用、选中的状态变化
  • 渐变、透明等视觉效果

这些都需要通过 Drawable 来实现。

贯穿 UI 架构层的一条铁律:颜色、Drawable、Style 等所有资源定义,都不要和业务挂钩;命名表达的是 UI 职能职能 ≠ 业务。一旦用「登录页按钮」「订单失败红框」这类业务语义去命名或拆分资源,就等于把形态和颜色锁死在某个页面——可复用性会被立刻破坏。Drawable 层只描述形态、色系、交互状态与档位,业务含义由使用它的页面自己承担。

跨平台:Android 的 Drawable/selector 对应 iOS 的 Asset Catalog 状态图、Web 的 design-token + CSS 状态类;命名与分层思想一致。

本文要点

  1. 为什么需要 Drawable 层规范
  2. Drawable 层的三层架构设计
  3. 统一的命名规范
  4. 形态层和组件层的设计要点

一、为什么需要 Drawable 层规范?

1.1 反例:混乱的 Drawable 管理

在我接触过的项目中,经常会看到这样的 Drawable 定义:

<!-- btn_primary.xml -->
<shape>
    <solid android:color="#FF4B00" />
    <corners android:radius="24dp" />
</shape>

<!-- btn_primary_pressed.xml -->
<shape>
    <solid android:color="#CC3D00" />
    <corners android:radius="24dp" />
</shape>

<!-- btn_disabled.xml -->
<shape>
    <solid android:color="#CCCCCC" />
    <corners android:radius="24dp" />
</shape>

Enter fullscreen mode Exit fullscreen mode

1.2 问题分析

问题类型 具体表现 影响
重复定义 每个状态都要单独定义一个文件 维护成本高
颜色硬编码 直接写色值,无法跟随主题切换 主题适配困难
命名混乱 btn_primarybutton_normal 等命名不统一 新人难以选择
难以维护 改一个圆角需要修改所有文件 修改成本高

1.3 根本原因

这些问题的本质在于:形态定义与颜色、状态过度耦合。当一个 Drawable 文件同时承载了形状、颜色和状态逻辑时,任何改动都会变得非常困难。


二、Drawable 层的三层架构

借鉴颜色体系的设计思路,我们同样采用三层架构来管理 Drawable:

2.1 核心原则

层级 职责 特点
组件层 组合状态和形态 通过 selector 引用 bg_*,实现状态切换
形态层 定义圆角、描边、填充结构 颜色只引用 func_*,禁止硬编码色值
颜色层 提供颜色 token 设计师维护的功能色,支持主题切换

与业务解耦:三层都只表达「UI 语言」,不表达「业务故事」。sel_orange_interact_capsule_emphasis_default 可以在下单主按钮、会员开通按钮、活动页主按钮上复用;若做成 sel_order_submit_primary,换页面就要复制一套 Drawable,主题与形态也无法在全 App 统一维护。


三、命名规范:让每个文件都有明确的含义

3.1 命名公式

{类型}_{色系}_{用途}_{状态}_{档位}

Enter fullscreen mode Exit fullscreen mode

3.2 组成说明

组成部分 说明 允许值
类型 Drawable 类别 bg(背景)、sel(选择器)、ic(图标)
色系 颜色分类 grayorangeredbluegreenblackwhite
用途 使用场景 surfacefillstrokefieldinteractgradient
状态 交互状态 idlealerthintemphasisneutraldefault
档位 优先级 数字 1(主要)、2(次要)、3(第三)

3.3 命名示例

bg_orange_fill_interact_1    // 橙色交互填充背景,主档位
bg_gray_surface_card_1       // 灰色卡片表面背景
sel_orange_interact_outline_emphasis_default  // 橙色描边交互选择器
bg_black_fill_scrim_1        // 黑色遮罩填充

Enter fullscreen mode Exit fullscreen mode

3.4 反例:业务绑定如何毁掉复用

❌ 错误:文件名承载业务,无法跨场景复用
bg_login_page_primary_btn.xml
bg_order_detail_pay_btn.xml
sel_profile_avatar_upload_error.xml

Enter fullscreen mode Exit fullscreen mode

三个文件往往只是圆角、色值、状态略有相似,却无法合并维护。正确做法是共用 sel_orange_interact_capsule_emphasis_default职能 + 形态命名,由不同页面的布局或 Style 决定用在哪里。


四、形态层设计:结构 + 功能色引用

4.1 基础形状类型

形状 用途 示例
fill 实心填充 按钮背景、卡片背景
stroke 描边空心 输入框边框、幽灵按钮
flat 扁平透明 文字按钮、点击区域
gradient 渐变效果 渐变背景、光晕效果
surface 表面效果 卡片、浮层、阴影

4.2 形状定义规范

<!-- bg_orange_fill_interact_1.xml -->
<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="rectangle">
    <solid android:color="@color/func_orange_bg_1" />
    <corners android:radius="@dimen/draw_corner_capsule" />
</shape>

<!-- bg_orange_stroke_interact_1.xml -->
<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="rectangle">
    <solid android:color="@color/func_clear_1" />  <!-- 透明填充 -->
    <stroke 
        android:width="@dimen/draw_stroke_reg" 
        android:color="@color/func_orange_border_1" />
    <corners android:radius="@dimen/draw_corner_capsule" />
</shape>

Enter fullscreen mode Exit fullscreen mode

关键设计点

  • 使用 @dimen 引用尺寸(由设计师在 dimens 中定义),避免硬编码
  • 使用 @color/func_* 引用功能色,支持主题切换;禁止在 drawable 中写 #RRGGBB 或直接引用 t_*、基础色
  • 同一套圆角/描边结构可搭配不同色系的功能色,由设计师在 token 层切换映射

五、状态色设计:状态逻辑放在 Drawable 层

在设计这套体系时,我们曾讨论过是否需要添加「禁用态」「选中态」「按下态」等状态相关的颜色到颜色层。

结论:状态色应该放在 drawable 层,而不是颜色层。

状态类型 建议位置 原因
禁用态 drawable层 通过selector组合实现
选中态 drawable层 通过selector组合实现
按下态 drawable层 通过selector组合实现

5.1 为什么不在颜色层定义状态色?

  1. 职责分离:颜色层负责提供「原料」,drawable层负责组合「成品」
  2. 灵活性:同一个颜色可以在不同状态下有不同表现
  3. 可复用性:一套颜色可以组合出多种状态效果

5.2 状态效果的正确实现方式

<!-- drawable/sel_orange_interact_capsule_emphasis_default.xml -->
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <!-- 禁用态 -->
    <item android:state_enabled="false">
        <shape>
            <solid android:color="@color/func_black_bg_2" />  <!-- 禁用态用灰色背景 -->
            <corners android:radius="24dp" />
        </shape>
    </item>
    <!-- 按下态 -->
    <item android:state_pressed="true">
        <shape>
            <solid android:color="@color/func_orange_bg_2" />  <!-- 按下态用浅色 -->
            <corners android:radius="24dp" />
        </shape>
    </item>
    <!-- 常态 -->
    <item>
        <shape>
            <solid android:color="@color/func_orange_bg_1" />  <!-- 常态用主色 -->
            <corners android:radius="24dp" />
        </shape>
    </item>
</selector>

Enter fullscreen mode Exit fullscreen mode


六、组件层设计:状态组合

6.1 Selector 的规范写法

<!-- sel_orange_interact_capsule_emphasis_default.xml -->
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <!-- 禁用态 -->
    <item 
        android:drawable="@drawable/bg_gray_fill_interact_2" 
        android:state_enabled="false" />
    <!-- 按下态 -->
    <item 
        android:drawable="@drawable/bg_orange_fill_interact_2" 
        android:state_pressed="true" />
    <!-- 常态 -->
    <item android:drawable="@drawable/bg_orange_fill_interact_1" />
</selector>

Enter fullscreen mode Exit fullscreen mode

6.2 状态优先级

Selector 中状态判断优先级(高 → 低):禁用态 > 选中态 > 按下态 > 焦点态 > 常态

6.3 输入框状态示例

<!-- sel_gray_field_rect_idle_default.xml -->
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item 
        android:drawable="@drawable/bg_gray_field_rect_2" 
        android:state_enabled="false" />
    <item 
        android:drawable="@drawable/bg_blue_field_rect_1" 
        android:state_focused="true" />
    <item android:drawable="@drawable/bg_gray_field_rect_1" />
</selector>

Enter fullscreen mode Exit fullscreen mode


七、常用组件的 Drawable 实现

7.1 按钮组件

按钮类型 Drawable 配置
主按钮 sel_orange_interact_capsule_emphasis_default
次按钮 sel_gray_interact_capsule_neutral_default
文字按钮 sel_gray_interact_flat_neutral_default
幽灵按钮 sel_orange_interact_outline_emphasis_default

7.2 输入框组件

状态 Drawable 配置
常态 sel_gray_field_rect_idle_default
警告态 sel_yellow_field_rect_hint_static
错误态 sel_red_field_rect_alert_static

7.3 卡片组件

卡片类型 Drawable 配置
普通卡片 bg_gray_surface_card_1
浮层卡片 bg_gray_surface_float_1
面板卡片 bg_gray_surface_panel_1

八、尺寸管理:统一的 Dimen 规范

8.1 圆角尺寸

<!-- dimens_drawable.xml -->
<dimen name="draw_corner_none">0dp</dimen>
<dimen name="draw_corner_sm">4dp</dimen>
<dimen name="draw_corner_reg">8dp</dimen>
<dimen name="draw_corner_lg">12dp</dimen>
<dimen name="draw_corner_capsule">999dp</dimen>  <!-- 胶囊形 -->

Enter fullscreen mode Exit fullscreen mode

8.2 描边尺寸

<dimen name="draw_stroke_thin">0.5dp</dimen>
<dimen name="draw_stroke_reg">1dp</dimen>
<dimen name="draw_stroke_thick">2dp</dimen>

Enter fullscreen mode Exit fullscreen mode

8.3 阴影尺寸

<dimen name="draw_shadow_elevation_1">2dp</dimen>
<dimen name="draw_shadow_elevation_2">4dp</dimen>
<dimen name="draw_shadow_elevation_3">8dp</dimen>

Enter fullscreen mode Exit fullscreen mode


九、与颜色体系的集成

9.1 颜色引用规范

<!-- ✅ 正确:引用功能色层 -->
<solid android:color="@color/func_orange_bg_1" />
<stroke android:color="@color/func_orange_border_1" />

<!-- ❌ 错误:直接引用主题色或基础色 -->
<solid android:color="@color/t_orange_4" />  <!-- 不推荐 -->
<solid android:color="@color/orange_4" />    <!-- 禁止 -->

Enter fullscreen mode Exit fullscreen mode

9.2 夜间模式自动适配

得益于三层颜色架构,Drawable 层无需任何修改即可支持夜间模式:

日间模式:
func_orange_bg_1 → t_orange_4 → orange_4 (#FF4B00)

夜间模式:
func_orange_bg_1 → t_orange_4 → orange_3b (#FF8833)

Enter fullscreen mode Exit fullscreen mode


十、与 View 的集成方式

10.1 通过 Style 引用

<!-- styles_button.xml -->
<style name="Btn.Orange.Capsule.Emphasis" parent="BaseButton">
    <item name="android:background">@drawable/sel_orange_interact_capsule_emphasis_default</item>
    <item name="android:textColor">@color/func_white_text_1</item>
</style>

Enter fullscreen mode Exit fullscreen mode

10.2 在布局中直接使用

<Button
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="@drawable/sel_orange_interact_capsule_emphasis_default"
    android:text="主操作" />

Enter fullscreen mode Exit fullscreen mode


十一、完整架构总结


十二、总结

Drawable 层是连接颜色和 UI 组件的桥梁,它的设计直接影响到:

  • UI 的一致性和美观度
  • 代码的可维护性
  • 主题切换的灵活性

通过遵循以下原则,你可以构建一套优秀的 Drawable 体系:

  1. 与业务解耦:Drawable 只表达形态与交互语义,绝不使用页面名、流程名、业务事件名
  2. 分离关注点:形态结构与填色引用分离,状态组合与组件分离
  3. 统一命名规范{类型}_{色系}_{用途}_{状态}_{档位}
  4. 复用优先:避免重复定义,提高资源利用率
  5. 与颜色体系集成:只引用设计师定义的 func_*,支持主题切换

参考代码

💡 如果你觉得这篇文章对你有帮助,欢迎点赞、收藏、转发。关注我,获取更多 Android 架构设计干货。

系列文章