把android资源类型详解

6 阅读3分钟

一、Android 主要资源类型

类型目录
动画anim/
属性动画animator/
颜色color/values/colors.xml
图片/形状drawable/
布局layout/
菜单menu/
字符串values/strings.xml
样式/主题values/styles.xml
尺寸values/dimens.xml
数组values/arrays.xml
字体font/
原始文件raw/
任意XMLxml/

二、四种常用布局资源

布局特点适用场景
LinearLayout水平/垂直线性排列简单列表、表单
RelativeLayout相对定位(相对于父容器或其他控件)中等复杂度的相对布局
FrameLayout堆叠,默认左上角Fragment容器、叠加效果
ConstraintLayout约束定位,扁平化,性能好复杂布局,替代多层嵌套

1、LinearLayout(线性布局)

核心特点

  • 子视图按水平垂直方向线性排列
  • 支持权重(weight) 分配剩余空间

关键属性

属性说明
orientationhorizontal(水平)/ vertical(垂直)
layout_weight权重值,按比例分配剩余空间
gravity子视图在布局中的对齐方式
layout_gravity自身在父布局中的对齐方式

示例

xml

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="标题" />

    <Button
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1"
        android:text="按钮" />

</LinearLayout>

适用场景:简单列表、表单页面
注意事项:嵌套过深会影响性能,建议控制层级


2. RelativeLayout(相对布局)

核心特点:通过相对关系定位(相对于父容器或其他控件),布局扁平。

关键属性

  • 相对于父容器layout_alignParentToplayout_centerInParent 等
  • 相对于其他控件layout_abovelayout_belowlayout_toLeftOf 等
  • 对齐属性layout_alignToplayout_alignBottom 等

示例代码

xml

<RelativeLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <TextView
        android:id="@+id/title"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
        android:text="标题" />

    <Button
        android:id="@+id/btn"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@id/title"
        android:layout_centerHorizontal="true"
        android:text="确定" />

</RelativeLayout>

适用场景:中等复杂度的相对定位布局
注意事项:需要为参照控件设置 id


3、FrameLayout(帧布局)

核心特点

  • 子视图默认堆叠在左上角
  • 后添加的覆盖先添加的
  • 结构简单,通常只放一个子视图

关键属性

属性说明
foreground前景图片,始终在最上层
foregroundGravity前景图片对齐方式
layout_gravity子视图在容器中的位置

示例

xml

<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/bg" />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:text="加载中" />

</FrameLayout>

适用场景

  • Fragment 容器
  • 叠加层(进度条、遮罩)
  • 单个子视图的简单布局

4、ConstraintLayout(约束布局)

核心特点

  • 通过约束定义位置关系
  • 扁平化设计,避免多层嵌套
  • Google 官方推荐的主流布局

关键属性

属性说明
layout_constraintLeft_toLeftOf左边与目标左边对齐
layout_constraintLeft_toRightOf左边位于目标右边
layout_constraintTop_toBottomOf顶部位于目标底部
layout_constraintBottom_toTopOf底部位于目标顶部
layout_constraintStart_toEndOf推荐使用,支持 RTL
layout_constraintHorizontal_bias水平偏向(0~1)
layout_constraintWidth_percent宽度百分比

示例

xml

<androidx.constraintlayout.widget.ConstraintLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <TextView
        android:id="@+id/title"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:text="标题"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <TextView
        android:id="@+id/content"
        android:layout_width="0dp"
        android:layout_height="0dp"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintTop_toBottomOf="@id/title"
        app:layout_constraintBottom_toBottomOf="parent" />

</androidx.constraintlayout.widget.ConstraintLayout>

适用场景:复杂布局、替代多层嵌套
注意事项:需要添加依赖 androidx.constraintlayout:constraintlayout