Android布局示例及常用属性
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent" <!-- 宽度充满父容器 -->
android:layout_height="match_parent"
android:orientation="vertical"
android:padding="16dp">
<TextView
android:id="@+id/title" <!-- 唯一ID标识 -->
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="用户登录"
android:textSize="24sp"
android:textColor="#000000"
android:layout_gravity="center"
android:layout_marginBottom="20dp"/>
<EditText
android:id="@+id/et_username"
android:layout_width="match_parent" <!-- 宽度充满父容器 -->
android:layout_height="wrap_content"
android:hint="请输入用户名"
android:inputType="text"
android:padding="12dp"
android:background="@drawable/edittext_bg"
android:layout_marginBottom="10dp"/>
<EditText
android:id="@+id/et_password"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="请输入密码"
android:inputType="textPassword" <!-- 密码输入类型 -->
android:padding="12dp"
android:background="@drawable/edittext_bg"
android:layout_marginBottom="20dp"/>
<Button
android:id="@+id/btn_login"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="登录"
android:textColor="#FFFFFF" <!-- 白色文字 -->
android:backgroundTint="#6200EE"
android:onClick="onLoginClick"
android:layout_marginTop="10dp"/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" <!-- 子元素水平排列 -->
android:gravity="center">
<TextView
android:id="@+id/tv_forgot"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="忘记密码?"
android:textColor="#6200EE"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="|"
android:layout_marginHorizontal="8dp"/>
<TextView
android:id="@+id/tv_register"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="注册账号"
android:textColor="#6200EE"/>
</LinearLayout>
</LinearLayout>
| 属性名称 |
说明 |
| android:layout_width |
控件宽度(match_parent充满父容器,wrap_content自适应内容,固定值) |
| android:layout_height |
控件高度(用法同上) |
| android:id |
唯一资源ID标识(@+id/控件名) |
| android:layout_margin |
外边距(可单独设置left/right/top/bottom) |
| android:padding |
内边距(可单独设置left/right/top/bottom) |
| android:orientation |
布局方向(vertical垂直排列,horizontal水平排列) |
| android:text |
显示文本内容("文本"或@string/资源) |
| android:background |
背景(颜色#FFFFFF或图片@drawable/资源) |
| android:layout_gravity |
在父容器中的对齐方式(center,right,bottom等) |
| android:gravity |
子元素对齐方式(center,start,end等) |
常见控件及事件说明
| 控件类型 |
控件名称(Android) |
典型事件(Kotlin风格) |
触发时机 |
常见应用场景 |
| 按钮 |
Button |
onClick |
鼠标点击或键盘回车确认时触发 |
提交表单、触发操作 |
|
ImageButton |
onMouseEnter/onMouseLeave |
鼠标悬停/离开控件区域时触发 |
显示悬停提示、动态效果 |
| 文本框 |
EditText |
onTextChanged |
文本内容发生改变时触发 |
实时搜索、输入验证 |
|
TextInputEditText |
onKeyDown/onKeyUp |
键盘按键按下/释放时触发 |
快捷键处理、输入限制 |
| 复选框 |
CheckBox |
onCheckedChange |
选中状态改变时触发 |
选项切换、条件过滤 |
| 下拉列表 |
Spinner |
onSelectedIndexChange |
选中项发生变化时触发 |
级联选择、数据分类 |
| 单选框组 |
RadioGroup |
onCheckedChange |
选项变更时触发(需判断当前选中项) |
单选设置、模式切换 |
| 滑块 |
SeekBar |
onValueChange |
滑块数值变化时触发 |
音量调节、参数实时调整 |
| 菜单项 |
MenuItem |
onClick |
点击菜单项时触发 |
导航、命令执行 |
| 列表项 |
ListView |
onSelectedIndexChange |
选中项变化时触发 |
详情展示、数据操作 |
|
RecyclerView |
onItemClick |
点击单项时触发(部分框架特有) |
交互式列表操作 |
| 图片 |
ImageView |
onClick |
点击图片时触发 |
图片查看器、交互式图鉴 |
| 超链接 |
TextView |
onClick |
点击链接时触发(可取消默认跳转行为) |
自定义导航、统计点击 |
| 底部导航 |
BottomNavigationView |
setOnItemSelectedListener / setOnItemReselectedListener |
选中新的导航项 / 重复点击当前已选中的项时触发 |
应用主要模块的一级页面切换 |
事件处理代码示例
button1.setOnClickListener