Android 资源管理全解析:Color、String、Style、Dimen、Array
1. 资源(Resource)概述
在 Android 开发中,资源文件(res/values 目录)用于存储 颜色、文本、样式、尺寸、数组 等 非代码 内容,便于:
- 统一管理 UI 风格,增强可维护性
- 支持 多语言(国际化)
- 适配 不同屏幕密度、尺寸、模式(夜间模式等)
- 提高应用的 复用性和灵活性
2. 颜色资源(Color)
2.1 定义颜色(colors.xml)
颜色资源用于 定义 UI 组件的颜色,可存储在 res/values/colors.xml
:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
| <?xml version="1.0" encoding="utf-8"?> <resources> <!-- 基础颜色 --> <color name="black">#000000</color> <color name="white">#FFFFFF</color>
<!-- 主题颜色 --> <color name="primary">#6200EE</color> <color name="primaryVariant">#3700B3</color> <color name="secondary">#03DAC6</color>
<!-- 自定义动态色彩 --> <color name="md_theme_light_primary">#6750A4</color> <color name="md_theme_dark_primary">#D0BCFF</color> </resources>
|
2.2 颜色的引用
(1) 在 XML 布局中
1 2 3 4
| <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:textColor="@color/primary" />
|
(2) 在 styles.xml
1
| <item name="android:textColor">@color/primary</item>
|
(3) 在 Kotlin 代码中
1 2
| val color = ContextCompat.getColor(context, R.color.primary) textView.setTextColor(color)
|
2.3 适配夜间模式(values-night/colors.xml)
1 2 3
| <resources> <color name="primary">#BB86FC</color> <!-- 夜间模式颜色 --> </resources>
|
3. 字符串资源(String)
3.1 定义字符串(strings.xml)
字符串资源存放在 res/values/strings.xml
,用于 文本国际化、多语言适配:
1 2 3 4 5 6 7 8 9 10 11
| <?xml version="1.0" encoding="utf-8"?> <resources> <string name="app_name">My Material3 App</string> <string name="hello_world">Hello, Material3!</string>
<!-- 带格式参数 --> <string name="welcome_message">Welcome, %1$s!</string>
<!-- HTML 富文本 --> <string name="html_text"><![CDATA[<b>Bold Text</b>]]></string> </resources>
|
3.2 字符串的引用
(1) 在 XML 布局中
1 2 3 4
| xml复制编辑<TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/hello_world" />
|
(2) 在 Kotlin 代码中
1
| val welcome = getString(R.string.welcome_message, "Alice")
|
(3) 支持多语言
在 res/values-es/strings.xml
定义西班牙语:
1 2 3
| <resources> <string name="hello_world">¡Hola, Material3!</string> </resources>
|
4. 样式资源(Style)
4.1 定义样式(styles.xml)
styles.xml
允许 复用 UI 属性,减少代码冗余:
1 2 3 4 5 6 7 8
| <?xml version="1.0" encoding="utf-8"?> <resources> <!-- 自定义按钮样式 --> <style name="CustomButton" parent="Widget.Material3.Button"> <item name="android:backgroundTint">@color/primary</item> <item name="android:textColor">@color/white</item> </style> </resources>
|
4.2 样式的引用
(1) 在 XML 布局中
1 2 3 4 5
| <Button style="@style/CustomButton" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Click Me" />
|
(2) 主题(Theme)
themes.xml
设定全局主题:
1 2 3 4
| <style name="Theme.MaterialTest" parent="Theme.Material3.Light.NoActionBar"> <item name="colorPrimary">@color/primary</item> <item name="android:buttonStyle">@style/CustomButton</item> </style>
|
(3) 在代码中应用
1
| button.setTextAppearance(R.style.CustomButton)
|
5. 尺寸资源(Dimen)
5.1 定义尺寸(dimens.xml)
dimens.xml
用于定义 像素、dp、sp、pt 等尺寸:
1 2 3 4 5 6
| <?xml version="1.0" encoding="utf-8"?> <resources> <dimen name="padding_small">8dp</dimen> <dimen name="padding_medium">16dp</dimen> <dimen name="text_size_large">24sp</dimen> </resources>
|
5.2 尺寸的引用
(1) 在 XML 布局中
1 2 3 4 5
| <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:textSize="@dimen/text_size_large" android:padding="@dimen/padding_medium" />
|
(2) 在 Kotlin 代码中
1 2
| val padding = resources.getDimension(R.dimen.padding_small) textView.setPadding(padding.toInt(), padding.toInt(), padding.toInt(), padding.toInt())
|
5.3 适配不同屏幕密度
values-sw600dp/dimens.xml
:适配 大屏
values-land/dimens.xml
:适配 横屏
6. 数组资源(Array)
6.1 定义数组(arrays.xml)
arrays.xml
用于存储 字符串、颜色或整数数组:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
| <?xml version="1.0" encoding="utf-8"?> <resources> <!-- 字符串数组 --> <string-array name="fruit_array"> <item>Apple</item> <item>Banana</item> <item>Cherry</item> </string-array>
<!-- 颜色数组 --> <array name="color_array"> <item>@color/primary</item> <item>@color/secondary</item> </array> </resources>
|
6.2 数组的引用
(1) 在 Kotlin 代码中
1 2
| 复制编辑val fruits = resources.getStringArray(R.array.fruit_array) val colors = resources.obtainTypedArray(R.array.color_array)
|
(2) 在 Spinner 中
1 2 3 4
| <Spinner android:layout_width="wrap_content" android:layout_height="wrap_content" android:entries="@array/fruit_array" />
|
7. 总结与优化建议
- Color:定义在
colors.xml
,配合 夜间模式 使用 values-night/colors.xml
- String:所有文本集中管理,支持多语言
- Style:提高 UI 复用性,避免重复代码
- Dimen:统一管理尺寸,支持不同屏幕适配
- Array:用于存储列表数据,如 Spinner 下拉项
掌握 color、string、style、dimen、array
资源管理,让你的 Android 代码更整洁、易维护,并支持 Material3 最新特性! 🚀