欢迎光临
我们一直在努力

Android_Studio学习笔记-01

一、尺寸与单位

1. px(像素)

  • 定义:屏幕上的物理像素点。

  • 特点:

    • 与实际屏幕像素一一对应。

    • 在不同密度的屏幕上,相同 px值的元素显示的实际物理尺寸可能不同(高密度屏上更小)。

  • 使用场景:通常不建议直接用于布局,除非处理自定义视图或精确像素操作。

2. dp(密度无关像素)

  • 定义:虚拟像素单位,基于屏幕密度自动缩放。

  • 换算:

    • 在基准密度 160 dpi​ 的屏幕上,1 dp = 1 px。

    • 换算公式:

    • dpi(Dots Per Inch,每英寸点数),dpi​ 是 屏幕物理密度的度量单位,表示每英寸长度上包含的物理像素点数量。

Android 将屏幕密度分为几个标准档位(称为 密度桶),方便开发者适配:

密度名称

dpi范围

比例因子

示例设备

ldpi

~120 dpi

0.75x

已淘汰的老旧设备

mdpi

~160 dpi

1x

基准密度​

hdpi

~240 dpi

1.5x

中低端设备

xhdpi

~320 dpi

2x

主流设备(如早期1080P)

xxhdpi

~480 dpi

3x

主流设备(如多数1080P+)

xxxhdpi

~640 dpi

4x

高端设备(2K/4K屏)

  • 特点:

    • 使元素在不同密度的屏幕上保持相近的物理尺寸。

    • 系统根据屏幕密度自动转换。

  • 使用场景:推荐用于布局尺寸(宽度、高度、边距等),但不包括文字。

3. sp(缩放无关像素)

  • 定义:基于 dp,但额外考虑用户的字体缩放偏好。

  • 特点:

    • 默认情况下,1 sp ≈ 1 dp。

    • 当用户在系统设置中调整字体大小时,使用 sp的文本会随之缩放。

  • 使用场景:专用于文字大小,以尊重用户可访问性设置。

总结

单位

全称

特点

使用场景

px

Pixels(像素)

物理像素,不随密度变化

不推荐常规布局

dp

Density-independent Pixels

根据屏幕密度缩放,保持物理尺寸一致

布局尺寸(宽高、边距等)

sp

Scale-independent Pixels

在 dp 基础上叠加用户字体缩放设置

字体大小

二、常用UI控件

1.TextView(文本显示)

1.1 基本结构

<TextView     android:id="@+id/text_view"          <!– 控件ID –>     android:layout_width="wrap_content"  <!– 宽度 –>     android:layout_height="wrap_content" <!– 高度 –>     android:text="显示文本"               <!– 文本内容 –>     android:textColor="#FF0000"          <!– 文字颜色 –>     android:textSize="16sp"              <!– 文字大小 –>     android:textStyle="bold"             <!– 文字样式 –>     android:gravity="center"             <!– 文字对齐方式 –>     android:maxLines="2"                 <!– 最大行数 –>     android:ellipsize="end" />           <!– 省略号位置 –>

1.2 尺寸控制

<!– 常用宽度/高度设置 –> android:layout_width="wrap_content"  <!– 包裹内容 –> android:layout_width="match_parent"   <!– 匹配父容器 –> android:layout_width="200dp"         <!– 固定值 –>

<!– 最小/最大尺寸 –> android:minWidth="100dp" android:maxWidth="300dp" android:minHeight="50dp" android:maxHeight="150dp"

1.3 文本控制

<!– 文本内容 –> android:text="直接文本"                      <!– 硬编码(不推荐) –> android:text="@string/app_name"            <!– 引用字符串资源(推荐) –>

<!– 文本颜色 –> android:textColor="#FF0000"                <!– RGB颜色 –> android:textColor="#80FF0000"              <!– ARGB颜色(带透明度) –> android:textColor="@color/colorPrimary"   <!– 引用颜色资源 –>

<!– 文本大小 –> android:textSize="16sp"                    <!– 推荐使用sp单位 –>

<!– 文本样式 –> android:textStyle="bold"                   <!– 粗体 –> android:textStyle="italic"                 <!– 斜体 –> android:textStyle="bold|italic"            <!– 粗斜体 –>

<!– 字体 –> android:fontFamily="sans-serif"            <!– 无衬线字体 –> android:fontFamily="sans-serif-condensed"  <!– 压缩无衬线 –> android:fontFamily="monospace"             <!– 等宽字体 –> android:fontFamily="serif"                 <!– 衬线字体 –>

1.4 对齐与布局

<!– 文本在TextView内的对齐 –> android:gravity="center"                   <!– 水平垂直居中 –> android:gravity="center_horizontal"       <!– 水平居中 –> android:gravity="center_vertical"         <!– 垂直居中 –> android:gravity="top|left"                 <!– 左上对齐 –> android:gravity="bottom|right"            <!– 右下对齐 –>

<!– 行高与间距 –> android:lineHeight="20sp"                  <!– 行高(API 28+) –> android:lineSpacingExtra="4dp"            <!– 额外行间距 –> android:lineSpacingMultiplier="1.2"       <!– 行间距倍数 –>

<!– 内边距 –> android:padding="16dp"                     <!– 所有方向 –> android:paddingLeft="8dp"                 <!– 左内边距 –> android:paddingTop="8dp"                  <!– 上内边距 –> android:paddingRight="8dp"                <!– 右内边距 –> android:paddingBottom="8dp"               <!– 下内边距 –> android:paddingStart="8dp"                <!– 开始边内边距(支持RTL) –> android:paddingEnd="8dp"                  <!– 结束边内边距(支持RTL) –>

<!– 自动调整文字大小 –> android:autoSizeTextType="uniform"        <!– 均匀调整 –> android:autoSizeMinTextSize="12sp"        <!– 最小字号 –> android:autoSizeMaxTextSize="20sp"        <!– 最大字号 –> android:autoSizeStepGranularity="1sp"     <!– 调整粒度 –>

1.5 多行文本处理

<!– 行数控制 –> android:singleLine="false"                <!– 是否单行(已废弃,用maxLines) –> android:maxLines="3"                      <!– 最大显示3行 –> android:minLines="1"                      <!– 最小显示1行 –>

<!– 省略号位置 –> android:ellipsize="none"                  <!– 不省略 –> android:ellipsize="start"                 <!– 在开头省略 –> android:ellipsize="middle"                <!– 在中间省略 –> android:ellipsize="end"                   <!– 在末尾省略(默认) –> android:ellipsize="marquee"               <!– 跑马灯效果(需设置singleLine=true) –>

2.EditText(文本输入)

2.1 基本结构

<EditText     android:id="@+id/edit_text"     android:layout_width="match_parent"     android:layout_height="wrap_content"     android:hint="请输入内容"          <!– 提示文字 –>     android:text=""                   <!– 文本内容 –>     android:inputType="text"         <!– 输入类型 –>     android:maxLines="1"             <!– 最大行数 –>     android:imeOptions="actionDone"   <!– 输入法动作 –>     android:background="@android:drawable/editbox_background" />

2.2 文本相关属性

<!– 文本内容 –> android:text="默认文本"              <!– 默认显示的文本 –> android:hint="请输入…"             <!– 提示文本(hint) –> android:textColorHint="#999999"     <!– 提示文本颜色 –>

<!– 文本样式 –> android:textColor="#000000"         <!– 文本颜色 –> android:textSize="16sp"             <!– 文本大小 –> android:textStyle="normal"          <!– 文本样式 –> android:typeface="normal"           <!– 字体 –> android:fontFamily="sans-serif"     <!– 字体系列 –>

<!– 光标与选中 –> android:cursorVisible="true"        <!– 是否显示光标 –> android:textCursorDrawable="@drawable/cursor"  <!– 光标样式 –> android:textColorHighlight="#FF4081" <!– 选中文本的背景色 –> android:textSelectHandle="@drawable/select_handle"  <!– 选择手柄 –>

2.3 布局与尺寸

<!– 尺寸 –> android:layout_width="match_parent"  <!– 宽度 –> android:layout_height="wrap_content" <!– 高度 –> android:minHeight="48dp"            <!– 最小高度 –> android:minWidth="100dp"            <!– 最小宽度 –> android:minEms="10"                 <!– 最小字符数(基于'M'宽度) –>

<!– 内边距 –> android:padding="8dp"               <!– 内边距 –> android:paddingLeft="8dp"           <!– 左内边距 –> android:paddingTop="8dp"            <!– 上内边距 –> android:paddingRight="8dp"          <!– 右内边距 –> android:paddingBottom="8dp"         <!– 下内边距 –> android:paddingStart="8dp"          <!– 开始边内边距 –> android:paddingEnd="8dp"            <!– 结束边内边距 –>

<!– 外边距 –> android:layout_margin="8dp"         <!– 外边距 –> android:layout_marginLeft="8dp"     <!– 左外边距 –> android:layout_marginTop="8dp"      <!– 上外边距 –>

2.4 输入限制

<!– 行数限制 –> android:singleLine="true"           <!– 单行(已废弃,推荐用maxLines) –> android:maxLines="3"               <!– 最大显示行数 –> android:minLines="1"               <!– 最小显示行数 –>

<!– 字符限制 –> android:maxLength="100"            <!– 最大输入字符数 –> android:minEms="5"                 <!– 最小字符数 –> android:maxEms="20"                <!– 最大字符数 –>

<!– 滚动 –> android:scrollHorizontally="true"  <!– 允许水平滚动 –> android:scrollbars="vertical"      <!– 显示滚动条:vertical/horizontal/both/none –> android:overScrollMode="always"    <!– 滚动到边缘时的效果 –>

2.5 inputType 类型大全

<!– 文本类型 –> android:inputType="text"                     <!– 普通文本 –> android:inputType="textCapCharacters"        <!– 所有字符大写 –> android:inputType="textCapWords"             <!– 每个单词首字母大写 –> android:inputType="textCapSentences"         <!– 每句话首字母大写 –> android:inputType="textAutoCorrect"          <!– 自动纠正 –> android:inputType="textAutoComplete"         <!– 自动完成 –> android:inputType="textMultiLine"            <!– 多行文本 –> android:inputType="textImeMultiLine"         <!– 输入法多行(与textMultiLine一起用) –> android:inputType="textNoSuggestions"        <!– 不显示建议 –> android:inputType="textUri"                  <!– URI格式 –> android:inputType="textEmailAddress"         <!– 电子邮件地址 –> android:inputType="textEmailSubject"         <!– 邮件主题 –> android:inputType="textShortMessage"         <!– 短消息 –> android:inputType="textLongMessage"          <!– 长消息 –> android:inputType="textPersonName"           <!– 人名 –> android:inputType="textPostalAddress"        <!– 邮政地址 –> android:inputType="textPassword"             <!– 密码 –> android:inputType="textVisiblePassword"      <!– 可见密码 –> android:inputType="textWebEditText"          <!– 作为网页表单的文本 –> android:inputType="textFilter"               <!– 文本筛选过滤 –> android:inputType="textPhonetic"             <!– 拼音输入 –>

<!– 数值类型 –> android:inputType="number"                   <!– 数字 –> android:inputType="numberSigned"             <!– 有符号数字 –> android:inputType="numberDecimal"            <!– 带小数点的数字 –> android:inputType="phone"                    <!– 电话号码 –> android:inputType="datetime"                 <!– 日期时间 –> android:inputType="date"                     <!– 日期 –> android:inputType="time"                     <!– 时间 –>

<!– 组合使用 –> android:inputType="textCapWords|textAutoCorrect"  <!– 首字母大写+自动纠正 –> android:inputType="text|textMultiLine"            <!– 文本+多行 –> android:inputType="number|numberDecimal"          <!– 数字+小数 –>

3.Button(按钮)

3.1 基本结构

<Button     android:id="@+id/button"     android:layout_width="wrap_content"     android:layout_height="wrap_content"     android:text="点击按钮"     android:textColor="#FFFFFF"     android:textSize="16sp"     android:background="@color/colorPrimary"     android:padding="12dp" />

3.2 尺寸与布局

<!– 尺寸设置 –> android:layout_width="wrap_content"  <!– 包裹内容 –> android:layout_width="match_parent"   <!– 匹配父容器 –> android:layout_width="100dp"         <!– 固定宽度 –>

android:layout_height="wrap_content" android:layout_height="match_parent" android:layout_height="48dp"         <!– Material Design 推荐高度 –>

<!– 外边距 –> android:layout_margin="8dp" android:layout_marginLeft="8dp" android:layout_marginTop="8dp" android:layout_marginRight="8dp" android:layout_marginBottom="8dp" android:layout_marginStart="8dp"     <!– 支持RTL布局 –> android:layout_marginEnd="8dp"

<!– 内边距 –> android:padding="12dp" android:paddingLeft="16dp" android:paddingTop="12dp" android:paddingRight="16dp" android:paddingBottom="12dp" android:paddingStart="16dp" android:paddingEnd="16dp"

<!– 最小尺寸 –> android:minWidth="88dp"             <!– Material Design 推荐最小宽度 –> android:minHeight="36dp"            <!– Material Design 推荐最小高度 –>

3.3 文本属性

<!– 文本内容 –> android:text="按钮文本" android:text="@string/button_text"  <!– 推荐使用字符串资源 –>

<!– 文本颜色 –> android:textColor="#FFFFFF"         <!– 颜色值 –> android:textColor="@color/white"    <!– 颜色资源 –>

<!– 文本大小 –> android:textSize="16sp"             <!– 推荐使用sp单位 –>

<!– 文本样式 –> android:textStyle="bold"            <!– 粗体 –> android:textStyle="italic"          <!– 斜体 –> android:textStyle="bold|italic"     <!– 粗斜体 –>

<!– 字体 –> android:fontFamily="sans-serif"     <!– 无衬线字体 –> android:fontFamily="sans-serif-medium" <!– 中等无衬线 –> android:typeface="normal"           <!– 正常 –> android:typeface="bold"             <!– 粗体 –> android:typeface="italic"           <!– 斜体 –> android:typeface="monospace"        <!– 等宽 –>

<!– 文本对齐 –> android:gravity="center"            <!– 文字居中 –> android:gravity="center_horizontal|center_vertical" <!– 水平垂直居中 –> android:gravity="start|center_vertical" <!– 开始对齐(支持RTL) –> android:gravity="end|center_vertical"   <!– 结束对齐(支持RTL) –>

<!– 文本阴影 –> android:shadowColor="#80000000"     <!– 阴影颜色 –> android:shadowDx="2"                <!– X轴偏移 –> android:shadowDy="2"                <!– Y轴偏移 –> android:shadowRadius="3"            <!– 模糊半径 –>

3.4 图标按钮

<!– 左侧图标 –> android:drawableLeft="@drawable/ic_add"      <!– 左侧图标 –> android:drawableStart="@drawable/ic_add"     <!– 开始位置图标(支持RTL) –>

<!– 右侧图标 –> android:drawableRight="@drawable/ic_arrow"   <!– 右侧图标 –> android:drawableEnd="@drawable/ic_arrow"     <!– 结束位置图标(支持RTL) –>

<!– 顶部图标 –> android:drawableTop="@drawable/ic_icon"      <!– 顶部图标 –>

<!– 底部图标 –> android:drawableBottom="@drawable/ic_icon"   <!– 底部图标 –>

<!– 图标大小 –> android:drawablePadding="8dp"                <!– 图标与文本间距 –> android:drawableTint="@color/white"          <!– 图标着色 –>

4.ImageView(图片显示)

4.1 基本结构

<ImageView     android:id="@+id/image_view"     android:layout_width="wrap_content"     android:layout_height="wrap_content"     android:src="@drawable/image"          <!– 图片源 –>     android:scaleType="centerCrop"         <!– 缩放类型 –>     android:adjustViewBounds="true"        <!– 调整视图边界 –>     android:maxWidth="300dp"               <!– 最大宽度 –>     android:maxHeight="300dp"              <!– 最大高度 –>     android:contentDescription="@string/image_desc" /> <!– 无障碍描述 –>

4.2 尺寸与布局

<!– 尺寸设置 –> android:layout_width="wrap_content"  <!– 包裹图片 –> android:layout_width="match_parent"   <!– 匹配父容器 –> android:layout_width="100dp"         <!– 固定宽度 –>

android:layout_height="wrap_content" android:layout_height="match_parent" android:layout_height="100dp"

<!– 最大最小尺寸 –> android:minWidth="50dp"              <!– 最小宽度 –> android:minHeight="50dp"             <!– 最小高度 –> android:maxWidth="300dp"             <!– 最大宽度 –> android:maxHeight="300dp"            <!– 最大高度 –>

<!– 调整视图边界(保持宽高比) –> android:adjustViewBounds="true"      <!– 根据图片宽高比调整视图大小 –>

<!– 内边距 –> android:padding="8dp"                <!– 内边距 –> android:paddingLeft="8dp" android:paddingTop="8dp" android:paddingRight="8dp" android:paddingBottom="8dp" android:paddingStart="8dp" android:paddingEnd="8dp"

<!– 外边距 –> android:layout_margin="8dp"          <!– 外边距 –> android:layout_marginLeft="8dp" android:layout_marginTop="8dp" android:layout_marginRight="8dp" android:layout_marginBottom="8dp" android:layout_marginStart="8dp" android:layout_marginEnd="8dp"

4.3 图片源

<!– 设置图片源 –> android:src="@drawable/image"        <!– drawable资源 –> android:src="@mipmap/icon"           <!– mipmap资源(应用图标) –> android:src="@color/colorAccent"     <!– 颜色资源(会显示为纯色块) –> android:src="@android:drawable/ic_menu_add"  <!– 系统drawable –>

<!– 背景图片(在src下方) –> android:background="@drawable/bg_image"

<!– 占位图和错误图(需结合图片加载库) –> app:placeholder="@drawable/placeholder"  <!– 占位图 –> app:error="@drawable/error_image"        <!– 错误图 –>

4.4 图片着色

<!– 图片着色 –> android:tint="#FF4081"               <!– 着色颜色 –> android:tint="@color/colorAccent"    <!– 着色颜色资源 –> android:tintMode="src_in"            <!– 着色模式 –> android:tintMode="multiply"          <!– 相乘模式 –> android:tintMode="screen"            <!– 滤色模式 –> android:tintMode="src_atop"          <!– 源在上方 –>

4.5 无障碍支持

<!– 无障碍描述(必须设置!) –> android:contentDescription="@string/image_description" android:contentDescription="商品主图"  <!– 直接设置 –> android:importantForAccessibility="yes"  <!– 无障碍重要性 –> android:importantForAccessibility="no"   <!– 不重要(如装饰性图片) –>

4.6 其他属性

<!– 裁剪到内边距 –> android:cropToPadding="true"         <!– 裁剪到内边距内 –>

<!– 基线对齐 –> android:baselineAlignBottom="true"   <!– 与底部基线对齐 –>

<!– 状态保持 –> android:saveEnabled="true"           <!– 是否保存状态 –> android:duplicateParentState="true"  <!– 复制父视图状态 –>

5.CheckBox & RadioButton(选择控件)

基本属性

<CheckBox     android:id="@+id/check_box"     android:layout_width="wrap_content"     android:layout_height="wrap_content"     android:text="同意协议"                <!– 文本 –>     android:checked="false"               <!– 默认选中状态 –>     android:button="@null"                <!– 移除默认选择框 –>     android:drawableStart="@drawable/custom_checkbox"  <!– 自定义选择框 –>     android:drawablePadding="8dp"         <!– 图标与文本间距 –>     android:buttonTint="@color/colorPrimary"  <!– 选择框着色 –>     android:checkedIcon="@drawable/checked_icon"  <!– 选中图标 –>     android:uncheckedIcon="@drawable/unchecked_icon"  <!– 未选中图标 –>     android:textColor="@color/black"      <!– 文本颜色 –>     android:textSize="16sp"               <!– 文本大小 –>     android:focusable="true"              <!– 是否可获取焦点 –>     android:clickable="true" />           <!– 是否可点击 –>

RadioButton 通常需要与 RadioGroup 一起使用,以实现单选功能。

<!– 必须放在 RadioGroup 中 –>
<RadioGroup
android:id="@+id/radio_group"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical" <!– 排列方向:vertical 或 horizontal –>
android:checkedButton="@+id/radio_button1" <!– 默认选中项 –>
android:buttonTint="@color/colorPrimary"> <!– 统一着色 –>

<RadioButton
android:id="@+id/radio_button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="选项一"
android:checked="true" /> <!– 默认选中 –>

<RadioButton
android:id="@+id/radio_button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="选项二" />

<RadioButton
android:id="@+id/radio_button3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="选项三" />
</RadioGroup>

 

 

赞(0)
未经允许不得转载:171主机测评 » Android_Studio学习笔记-01
分享到: 更多 (0)

评论 抢沙发

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址