一、UI 开发基础概念(初学者必看)
在学习具体控件前,先理解以下核心概念,能大幅降低后续学习难度:
1. View 与 ViewGroup 的关系
- View:所有 UI 控件的基类(如 Button、TextView),是一个 "可视元素"。
- ViewGroup:特殊的 View,可包含多个 View(如 LinearLayout、ConstraintLayout),是 "容器"。
- 关系:ViewGroup → View → 子 View → 子 ViewGroup → 子 View(形成树状结构)。
2. 尺寸单位
px
(像素):屏幕实际像素点,不推荐直接使用(不同设备像素密度不同,显示效果不一致)。dp
(密度无关像素):推荐单位,系统会根据屏幕密度自动转换为 px(如 1dp 在低密度屏幕≈1px,高密度≈2px)。sp
(缩放像素):专门用于文字大小,会随用户设置的字体大小缩放(建议文字用 sp,其他用 dp)。
3. 资源引用规则
在 XML 中引用资源的统一格式:
@[资源类型]/[资源名称] <!-- 引用已有资源 -->
@+[资源类型]/[资源名称] <!-- 定义新资源(如ID) -->
android:text="@string/hello" <!-- 引用strings.xml中的字符串 -->
android:src="@drawable/icon" <!-- 引用drawable目录下的图片 -->
android:id="@+id/btn_login" <!-- 定义新ID -->
二、核心控件属性详解(附示例)
1. 文本控件(TextView & EditText)
TextView 示例:
<TextView
android:id="@+id/tv_title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/welcome"
android:textSize="20sp"
android:textColor="#333333"
android:textStyle="bold"
android:maxLines="2"
android:ellipsize="end"
android:padding="10dp"
android:gravity="center"/>
EditText 示例:
<EditText
android:id="@+id/et_username"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="@string/input_username"
android:textSize="16sp"
android:inputType="textPersonName"
android:maxLength="20"
android:padding="12dp"
android:background="@drawable/round_edittext_bg"/>
2. 按钮控件(Button & ImageButton)
Button 示例:
<Button
android:id="@+id/btn_login"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/login"
android:textSize="18sp"
android:textColor="#FFFFFF"
android:background="@drawable/btn_selector" <!-- 状态选择器(按下/正常) -->
android:padding="14dp"
android:layout_marginTop="20dp"/>
ImageButton 示例:
<ImageButton
android:id="@+id/btn_back"
android:layout_width="48dp"
android:layout_height="48dp"
android:src="@drawable/ic_back"
android:background="@android:color/transparent" <!-- 透明背景 -->
android:contentDescription="@string/back"/>
3. 图片控件(ImageView)
示例(头像显示):
<ImageView
android:id="@+id/iv_avatar"
android:layout_width="80dp"
android:layout_height="80dp"
android:src="@drawable/default_avatar"
android:scaleType="centerCrop" <!-- 居中裁剪,避免变形 -->
android:background="@drawable/circle_bg" <!-- 圆形背景 -->
android:padding="2dp"/>
三、布局容器实战(重点难点)
1. LinearLayout 权重(weight)详解
场景:横向三个按钮,等宽分布
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<Button
android:layout_width="0dp" <!-- 必须设置为0dp -->
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="按钮1"/>
<Button
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="按钮2"/>
<Button
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="按钮3"/>
</LinearLayout>
原理:总权重为 3,每个按钮权重 1,各占 1/3 宽度。
2. ConstraintLayout 约束实践
场景:实现一个登录表单(用户名、密码、登录按钮)
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageView
android:id="@+id/iv_logo"
android:layout_width="100dp"
android:layout_height="100dp"
android:src="@drawable/logo"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.2"/> <!-- 垂直偏移20% -->
<EditText
android:id="@+id/et_username"
android:layout_width="0dp" <!-- 0dp表示受约束控制 -->
android:layout_height="wrap_content"
android:hint="用户名"
app:layout_constraintTop_toBottomOf="@id/iv_logo"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.5"
android:layout_marginTop="40dp"
android:layout_marginStart="30dp"
android:layout_marginEnd="30dp"/>
<EditText
android:id="@+id/et_password"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:hint="密码"
android:inputType="textPassword"
app:layout_constraintTop_toBottomOf="@id/et_username"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toEndOf="parent"
android:layout_marginTop="20dp"
android:layout_marginStart="30dp"
android:layout_marginEnd="30dp"/>
<Button
android:id="@+id/btn_login"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:text="登录"
app:layout_constraintTop_toBottomOf="@id/et_password"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toEndOf="parent"
android:layout_marginTop="40dp"
android:layout_marginStart="30dp"
android:layout_marginEnd="30dp"/>
</androidx.constraintlayout.widget.ConstraintLayout>
约束要点:
toTopOf
/toBottomOf
/toStartOf
/toEndOf
:指定相对于其他控件的位置。0dp
:表示宽度 / 高度由约束自动计算。bias
:偏移量(0-1),控制水平 / 垂直方向的位置。
四、自定义控件样式(提升 UI 颜值)
1. 自定义圆角按钮
创建res/drawable/btn_rounded.xml
:
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<solid android:color="#4CAF50"/> <!-- 填充色 -->
<corners android:radius="24dp"/> <!-- 圆角半径 -->
<padding
android:bottom="12dp"
android:left="24dp"
android:right="24dp"
android:top="12dp"/> <!-- 内边距 -->
</shape>
在 Button 中引用:
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="圆角按钮"
android:background="@drawable/btn_rounded"/>
2. 按钮状态选择器(按下 / 正常效果)
创建res/drawable/btn_selector.xml
:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed="true"
android:drawable="@drawable/btn_pressed"/> <!-- 按下状态 -->
<item android:state_focused="true"
android:drawable="@drawable/btn_focused"/> <!-- 聚焦状态 -->
<item android:drawable="@drawable/btn_normal"/> <!-- 默认状态 -->
</selector>
五、常见问题与解决方案(避坑指南)
1. 文本显示不全(超出屏幕)
原因 :未设置maxLines
或ellipsize
。
解决方案:
<TextView
android:maxLines="2"
android:ellipsize="end"/>
2. 图片显示变形
原因 :未设置scaleType
,默认fitXY
会拉伸图片。
解决方案:
<ImageView
android:scaleType="centerCrop"/> <!-- 常用裁剪模式 -->
3. 布局层级过深导致卡顿
优化方案:
- 使用 ConstraintLayout 减少嵌套。
- 使用
<merge>
标签合并布局层级。 - 复杂布局考虑使用
<ViewStub>
延迟加载。
六、学习资源推荐(初学者必收藏)
- 官方文档 :
- 优质教程 :
- UI 设计资源 :
- Material Design:官方设计语言指南。
- IconFont:免费图标库(适配 Android)。
总结
掌握 Android UI 开发的关键在于:
- 理解基础概念(View/ViewGroup、尺寸单位、资源引用)。
- 熟悉核心控件属性(特别是文本、按钮、图片控件)。
- 精通布局容器(LinearLayout 权重、ConstraintLayout 约束)。
- 多实践多试错(遇到问题先查官方文档,再看 Stack Overflow)。