Android 常用控件及核心属性

Android 常用控件及核心属性

  • 一、基础显示类控件
    • [1. TextView(文本显示控件)](#1. TextView(文本显示控件))
    • [2. ImageView(图片显示控件)](#2. ImageView(图片显示控件))
  • 二、交互操作类控件
    • [1. Button(按钮)](#1. Button(按钮))
    • [2. EditText(输入框)](#2. EditText(输入框))
    • [3. CheckBox(复选框)](#3. CheckBox(复选框))
    • [4. RadioButton + RadioGroup(单选按钮)](#4. RadioButton + RadioGroup(单选按钮))
    • [5. Switch(开关控件)](#5. Switch(开关控件))
    • [6. ProgressBar(进度条)](#6. ProgressBar(进度条))
  • [三、列表 / 容器类控件](#三、列表 / 容器类控件)
    • [1. RecyclerView(列表控件)](#1. RecyclerView(列表控件))
    • [2. ScrollView(滚动视图)](#2. ScrollView(滚动视图))
  • 四、通用属性

一、基础显示类控件

1. TextView(文本显示控件)

最基础的控件,用于显示文字、标题、说明等。

xml 复制代码
<TextView
    android:id="@+id/tv_title"  <!-- 控件唯一标识 -->
    android:layout_width="wrap_content"  <!-- 宽度:包裹内容/匹配父布局/固定值 -->
    android:layout_height="wrap_content" <!-- 高度:同上 -->
    android:text="Hello Android"  <!-- 显示的文字 -->
    android:textSize="16sp"       <!-- 文字大小(必须用sp单位) -->
    android:textColor="#FF0000"   <!-- 文字颜色 -->
    android:textStyle="bold"      <!-- 文字样式:bold粗体/italic斜体/bolditalic粗斜 -->
    android:gravity="center"      <!-- 文字在控件内居中/左/右对齐 -->
    android:maxLines="1"          <!-- 最大行数 -->
    android:ellipsize="end"       <!-- 文字超出末尾显示省略号 -->
    android:background="#FFFFFF"/> <!-- 背景色/背景图片 -->

2. ImageView(图片显示控件)

用于显示图片、图标、背景图。

xml 复制代码
<ImageView
    android:id="@+id/iv_icon"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:src="@mipmap/ic_launcher"  <!-- 显示的图片资源(核心) -->
    android:scaleType="centerCrop"     <!-- 图片缩放模式:填充/居中/裁剪等 -->
    android:adjustViewBounds="true"    <!-- 自适应图片宽高比 -->
    android:tint="#888888"/>            <!-- 图片着色(改变图标颜色) -->

二、交互操作类控件

1. Button(按钮)

继承自TextView,用于点击触发操作(提交、跳转、确认等)。

xml 复制代码
<Button
    android:id="@+id/btn_submit"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:text="提交"
    android:textSize="18sp"
    android:textColor="#FFFFFF"
    android:background="#3F51B5"  <!-- 按钮背景 -->
    android:onClick="clickSubmit"/> <!-- 点击事件(直接绑定方法) -->

拓展:AppCompatButton 兼容低版本,功能一致。

2. EditText(输入框)

继承自TextView,用于用户输入文字、密码、账号等。

xml 复制代码
<EditText
    android:id="@+id/et_account"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:hint="请输入账号"       <!-- 输入提示文字 -->
    android:inputType="text"       <!-- 输入类型:text文本/number数字/textPassword密码 -->
    android:maxLength="11"         <!-- 最大输入长度 -->
    android:textColorHint="#999999"<!-- 提示文字颜色 -->
    android:padding="10dp"         <!-- 内边距 -->
    android:singleLine="true"/>     <!-- 单行输入 -->

3. CheckBox(复选框)

用于多选

xml 复制代码
<CheckBox
    android:id="@+id/cb_agree"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="同意用户协议"
    android:checked="true"/>  <!-- 默认是否选中 -->

4. RadioButton + RadioGroup(单选按钮)

必须配合 RadioGroup 使用,实现单选。

xml 复制代码
<RadioGroup
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:orientation="horizontal"> <!-- 水平排列 -->

    <RadioButton
        android:id="@+id/rb_male"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="男"
        android:checked="true"/> <!-- 默认选中 -->

    <RadioButton
        android:id="@+id/rb_female"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="女"/>
</RadioGroup>

5. Switch(开关控件)

用于开关状态切换。

xml 复制代码
<Switch
    android:id="@+id/switch_notify"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="开启通知"
    android:checked="false"/> <!-- 默认关闭 -->

6. ProgressBar(进度条)

用于加载中、进度展示。

xml 复制代码
<!-- 圆形加载条(默认) -->
<ProgressBar
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"/>

<!-- 水平进度条 -->
<ProgressBar
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    style="?android:attr/progressBarStyleHorizontal"
    android:max="100"    <!-- 最大进度 -->
    android:progress="50"/> <!-- 当前进度 -->

三、列表 / 容器类控件

1. RecyclerView(列表控件)

最常用的列表控件(聊天记录、商品列表、新闻列表),替代老旧的 ListView。

核心作用:展示大量数据列表,支持滑动、复用优化。

2. ScrollView(滚动视图)

用于内容超出屏幕时滚动显示(长文本、详情页)。

xml 复制代码
<ScrollView
    android:layout_width="match_parent"
    android:layout_height="match_parent">
    <!-- 内部只能有一个子控件(如LinearLayout) -->
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical">
        <!-- 放入长内容 -->
    </LinearLayout>
</ScrollView>

四、通用属性

属性 作用 常用示例值
android:id 控件唯一标识 @+id/xxx
android:layout_width 控件宽度 match_parent(填满父布局) / wrap_content(包裹内容) / 200dp
android:layout_height 控件高度 同上
android:layout_margin 外边距(控件与外部间距) 10dp
android:padding 内边距(内容与控件边框间距) 10dp
android:background 背景(颜色 / 图片 / 形状) #FFFFFF / @drawable/xxx
android:visibility 显示 / 隐藏 visible(显示) / invisible(隐藏占位置) / gone(隐藏不占位置)
android:gravity 内容在控件内对齐 center / left / right
相关推荐
遥不可及zzz2 小时前
[特殊字符] Android AAB 一键安装工具配置指南
android·macos
私人珍藏库2 小时前
【Android】一键硬核锁手机
android·智能手机·app·工具·软件
TTTao23333 小时前
自用Android项目框架备份
android
沃尔威武3 小时前
性能调优实战:从火焰图定位到SQL优化的全流程
android·数据库·sql
Fᴏʀ ʏ꯭ᴏ꯭ᴜ꯭.4 小时前
基于MySQL一主一从环境添加多个新从库
android·mysql·adb
JJay.5 小时前
Android App Functions 深入理解
android
开发_李行6 小时前
简历对应知识点总结--专业技能5
android
网络安全许木6 小时前
自学渗透测试(1~6天工具使用的回温)
android
匆忙拥挤repeat6 小时前
Android Compose 状态保存的API总结
android