在 Android 开发中,资源(Resources) 是应用 UI 与内容的重要组成部分。合理使用资源不仅可以提高代码可读性,还能方便多语言、多分辨率适配。本文将先系统列举 Android 中常见的资源类型,然后重点详细介绍四种最常用的布局资源(Layout) 。
一、Android 常见资源类型汇总
Android 资源统一存放在 res/ 目录下,不同资源类型放在不同子目录中。
1、layout(布局资源)
用于定义界面结构,使用 XML 描述 UI 组件层级关系
目录:
res/layout/
示例:
activity_main.xml
fragment_home.xml
item_list.xml
2、drawable(图片资源)
用于存放图片、shape、selector 等图形资源
目录:
res/drawable/
支持类型:
- png / jpg / webp 图片
- shape 图形
- selector 状态选择器
- layer-list 图层
- vector 矢量图
示例:
bg_button.xml
ic_launcher.png
selector_btn.xml
3、mipmap(启动图标资源)
专门存放应用图标,适配不同分辨率
目录:
res/mipmap-mdpi
res/mipmap-hdpi
res/mipmap-xhdpi
示例:
ic_launcher.png
4、values(值资源)
存放字符串、颜色、尺寸、样式等
目录:
res/values/
常见文件:
strings.xml
<string name="app_name">Demo</string>
colors.xml
<color name="colorPrimary">#6200EE</color>
dimens.xml
<dimen name="text_size">16sp</dimen>
styles.xml
<style name="AppTheme" parent="Theme.AppCompat.Light"/>
5、menu(菜单资源)
定义菜单栏、弹出菜单
目录:
res/menu/
示例:
<menu>
<item
android:id="@+id/menu_add"
android:title="添加"/>
</menu>
6、anim(补间动画资源)
定义透明度、缩放、旋转等动画
目录:
res/anim/
示例:
alpha.xml
translate.xml
7、animator(属性动画资源)
目录:
res/animator/
示例:
object_animator.xml
8、raw(原始资源)
存放音频、视频、json 等
目录:
res/raw/
示例:
music.mp3
data.json
9、xml(自定义XML配置)
目录:
res/xml/
示例:
network_config.xml
provider_paths.xml
10、font(字体资源)
目录:
res/font/
示例:
roboto.ttf
二、布局资源四种常用类型(重点)
Android 最常用的四种布局:
- LinearLayout(线性布局)
- RelativeLayout(相对布局)
- ConstraintLayout(约束布局)
- FrameLayout(帧布局)
下面详细讲解。
三、LinearLayout(线性布局)
特点
按照水平或垂直方向依次排列子控件
属性:
android:orientation="vertical"
android:orientation="horizontal"
示例
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<TextView
android:text="用户名"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
</LinearLayout>
常用属性
1)orientation
控制排列方向
vertical 垂直排列
horizontal 水平排列
2)layout_weight(权重)
按比例分配空间
android:layout_weight="1"
示例:
<LinearLayout
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="match_parent">
<Button
android:layout_weight="1"
android:layout_width="0dp"
android:text="左"/>
<Button
android:layout_weight="1"
android:layout_width="0dp"
android:text="右"/>
</LinearLayout>
效果:左右各占一半
四、RelativeLayout(相对布局)
特点
子控件相对于父容器或其他控件定位
常用于复杂 UI
示例
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<Button
android:id="@+id/btn1"
android:text="按钮1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
<Button
android:text="按钮2"
android:layout_below="@id/btn1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
</RelativeLayout>
常用属性
相对父布局
layout_alignParentTop
layout_alignParentBottom
layout_centerInParent
layout_centerHorizontal
layout_centerVertical
相对控件
layout_below
layout_above
layout_toRightOf
layout_toLeftOf
layout_alignTop
layout_alignBottom
五、ConstraintLayout(约束布局)
特点
目前官方推荐布局
通过约束关系确定位置
性能优于多层嵌套布局
示例
<androidx.constraintlayout.widget.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent">
<Button
android:id="@+id/btn"
android:text="按钮"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintStart_toStartOf="parent"/>
</androidx.constraintlayout.widget.ConstraintLayout>
常用约束
上下约束
layout_constraintTop_toTopOf
layout_constraintTop_toBottomOf
layout_constraintBottom_toBottomOf
左右约束
layout_constraintStart_toStartOf
layout_constraintEnd_toEndOf
居中
左右都约束到 parent
上下都约束到 parent
居中示例
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toEndOf="parent"
六、FrameLayout(帧布局)
特点
所有控件叠加显示
后添加的控件覆盖前面的控件
常用于:
- Fragment容器
- 视频播放器
- 图片叠加
示例
<FrameLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageView
android:src="@drawable/bg"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
<TextView
android:text="文字叠加"
android:layout_gravity="center"
android:textSize="20sp"
android:textColor="#fff"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
</FrameLayout>
七、四种布局对比
| 布局 | 特点 | 使用场景 | 推荐度 |
|---|---|---|---|
| LinearLayout | 线性排列 | 简单列表 | ⭐⭐⭐⭐ |
| RelativeLayout | 相对定位 | 复杂UI | ⭐⭐⭐ |
| ConstraintLayout | 约束定位 | 所有复杂界面 | ⭐⭐⭐⭐⭐ |
| FrameLayout | 层叠布局 | Fragment容器 | ⭐⭐⭐⭐ |
八、布局选择建议
简单上下结构
→ LinearLayout
复杂界面
→ ConstraintLayout
需要覆盖叠加
→ FrameLayout
老项目维护
→ RelativeLayout
新项目开发
→ ConstraintLayout(推荐)
九、总结
Android 资源类型主要包括:
- layout 布局资源
- drawable 图片资源
- mipmap 图标资源
- values 值资源
- menu 菜单资源
- anim 动画资源
- animator 属性动画
- raw 原始资源
- xml 配置资源
- font 字体资源
其中最重要的是 layout 布局资源,常用的四种布局:
- LinearLayout
- RelativeLayout
- ConstraintLayout
- FrameLayout
掌握这四种布局即可完成 90% Android UI 开发。