一、Android 资源类型完整列表
Android 项目支持以下资源类型:
1. 动画资源 (Animation)
- 属性动画
- 补间动画(Tween Animation)
- 帧动画(Frame Animation)
2. 颜色资源
- 颜色值定义(十六进制、RGB 等)
3. 字符串资源
- 文本字符串
- 字符串数组
4. 样式与主题资源
- Style:针对单个组件的样式
- Theme:针对整个应用或 Activity 的主题
5. 布局资源
- XML 布局文件(定义界面结构)
6. 菜单资源
- 选项菜单
- 上下文菜单
- 弹出菜单
7. Drawable 资源
- 位图文件
- 九宫格图片
- 矢量图形
- 形状、图层列表、选择器等
8. 尺寸资源
- 尺寸值(支持 dp、sp、px 等单位)
9. 值资源
- 整数、浮点数
- 布尔值
- ID 资源
10. 原始资源
- 音频文件
- 视频文件
- 其他任意文件
11. 其他资源
- 动画列表
- 过渡动画
- 界面样式
- 字体资源
- 导航图资源
二、布局资源的四种常用类型详解
1. LinearLayout(线性布局)
特点:
- 最基础的布局,按照垂直或水平方向线性排列子视图
- 子视图依次排列,不支持重叠
常用属性:
<!-- 方向:垂直/水平 -->
android:orientation="vertical|horizontal"
<!-- 子视图权重 -->
android:layout_weight="1"
<!-- 重力:内部子视图对齐方式 -->
android:gravity="center"
适用场景:
- 简单的表单布局
- 列表项
- 需要按照顺序排列的界面
示例:
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<TextView android:text="标题" />
<EditText android:hint="请输入" />
<Button android:text="提交" />
</LinearLayout>
2. RelativeLayout(相对布局)
特点:
- 子视图通过相对位置关系进行定位
- 可以相对于父容器或其他子视图定位
- 支持复杂的对齐和位置关系
常用属性:
<!-- 相对于父容器 -->
android:layout_centerInParent="true"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
<!-- 相对于其他视图 -->
android:layout_above="@id/otherView"
android:layout_toRightOf="@id/otherView"
android:layout_below="@id/otherView"
适用场景:
- 复杂的界面布局
- 需要元素相互依赖位置的界面
- 重叠元素的布局
示例:
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="@+id/title"
android:layout_centerHorizontal="true"
android:text="标题" />
<Button
android:id="@+id/button"
android:layout_below="@id/title"
android:layout_centerHorizontal="true"
android:text="按钮" />
</RelativeLayout>
3. FrameLayout(帧布局)
特点:
- 最简单的布局,子视图按顺序堆叠
- 所有子视图默认从左上角开始
- 后添加的视图会覆盖在前面的视图之上
常用属性:
<!-- 子视图位置控制 -->
android:layout_gravity="center|bottom"
适用场景:
- 需要重叠显示的界面
- 加载进度覆盖层
- 图片上的图标或标记
- 简单的容器布局
示例:
<FrameLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:src="@drawable/background" />
<TextView
android:layout_gravity="center"
android:text="文字覆盖在图片上" />
</FrameLayout>
4. ConstraintLayout(约束布局)
特点:
- Android Studio 默认布局
- 通过约束关系定义视图位置
- 性能优异,减少布局层级
- 支持可视化编辑器
常用属性:
<!-- 约束关系 -->
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintTop_toTopOf="@id/otherView"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintBottom_toBottomOf="parent"
<!-- 宽高设置 -->
app:layout_constraintWidth_default="wrap|spread|percent"
app:layout_constraintWidth_percent="0.5"
<!-- 链条 -->
app:layout_constraintHorizontal_chainStyle="packed"
适用场景:
- 复杂的扁平化布局
- 需要高性能的界面
- 响应式设计
- 大多数现代应用界面
示例:
<ConstraintLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<Button
android:id="@+id/btn1"
android:layout_width="0dp"
android:layout_height="wrap_content"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent"
android:text="占满宽度" />
<Button
android:id="@+id/btn2"
android:layout_width="0dp"
android:layout_height="wrap_content"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toLeftOf="@id/btn3"
app:layout_constraintTop_toBottomOf="@id/btn1"
app:layout_constraintHorizontal_chainStyle="spread"
android:text="按钮2" />
<Button
android:id="@+id/btn3"
android:layout_width="0dp"
android:layout_height="wrap_content"
app:layout_constraintLeft_toRightOf="@id/btn2"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="@id/btn2"
android:text="按钮3" />
</ConstraintLayout>