Android 基础知识

Android 主页 Android 概述 Android 下载安装和开发环境配置 Android Studio IDE Android 架构 Android 应用程序组件 Android Hello World 示例 Android 资源 Android 活动 Android 服务 Android 广播接收器 Android 内容提供者 Android 片段 Android Intents/Filters

Android 用户界面

Android UI 布局 Android UI 控件 Android 事件处理 Android 样式和主题 Android 自定义组件

Android 高级概念

Android 拖放 Android 通知 Android 基于位置的服务 Android 发送电子邮件 Android 发送短信 Android 拨打电话 Android 发布应用程序

Android 实用示例

Android 警报对话框 Android 动画 Android 音频捕捉 Android 音频管理器 Android 自动完成 Android 最佳实践 Android 蓝牙 Android 相机 Android 剪贴板 Android 自定义字体 Android 数据备份 Android 开发者工具 Android 模拟器 Android Facebook 集成 Android 手势 Android 谷歌地图 Android 图像效果 Android 图像切换 Android 内部存储 Android JetPlayer Android JSON 解析器 Android Linkedin 集成 Android 旋转加载器 Android 本地化 Android 登录应用 Android 媒体播放器 Android 多点触控 Android 导航 Android 网络连接 Android NFC 指南 Android PHP/MySQL Android 进度圈 Android 进度条 Android 推送通知 Android 渲染脚本 Android RSS 阅读器 Android 屏幕投射 Android SDK 管理器 Android 传感器 Android 会话管理 Android 共享首选项 Android SIP 协议 Android 拼写检查器 Android SQLite 数据库 Android 支持库 Android 测试 Android 文字转语音 Android TextureView Android Twitter 集成 Android UI 设计 Android UI 模式 Android UI 测试 Android WebView 布局 Android Wi-Fi Android Widgets Android XML 解析器

Android 其他

Android 面试问题 Android 有用的资源 Android 测验


Android - UI 用户界面设计

在本章中,我们将了解 android 屏幕的不同 UI 组件。 本章还介绍了制作更好的 UI 设计的技巧,并解释了如何设计 UI。


UI 屏幕组件

Android 应用程序的典型用户界面由操作栏和应用程序内容区域组成。

  • 主操作栏
  • 查看区
  • 内容区
  • 拆分操作栏

这些组件也显示在下图中 −

Android UI 教程

了解屏幕组件

android 应用程序的基本单元是活动。 UI 在 xml 文件中定义。 在编译过程中,XML 中的每个元素都被编译成等效的 Android GUI 类,其属性由方法表示。

视图和视图组

活动由视图组成。 视图只是出现在屏幕上的一个小部件。 它可能是按钮等。 一个或多个视图可以组合成一个 GroupView。 ViewGroup 的示例包括布局。

布局类型

有许多类型的布局。 下面列出了其中一些 −

  • 线性布局
  • 绝对布局
  • 表格布局
  • 框架布局
  • 相对布局

线性布局

Linear layout 线性布局又分为水平布局和垂直布局。 这意味着它可以将视图排列在单列或单行中。 这是包含文本视图的线性布局(垂直)的代码。

<?xml version=”1.0” encoding=”utf-8”?>
<LinearLayout xmlns:android=”http://schemas.android.com/apk/res/android”
   android:layout_width=”fill_parent”
   android:layout_height=”fill_parent”
   android:orientation=”vertical” >
   
   <TextView
      android:layout_width=”fill_parent”
      android:layout_height=”wrap_content”
      android:text=”@string/hello” />
</LinearLayout>

绝对布局

AbsoluteLayout 绝对布局使您能够指定其子项的确切位置。 可以这样声明。

<AbsoluteLayout
   android:layout_width=”fill_parent”
   android:layout_height=”fill_parent”
   xmlns:android=”http://schemas.android.com/apk/res/android” >
   
   <Button
      android:layout_width=”188dp”
      android:layout_height=”wrap_content”
      android:text=”Button”
      android:layout_x=”126px”
      android:layout_y=”361px” />
</AbsoluteLayout>

表格布局

TableLayout 表格布局将视图分组为行和列。 可以这样声明。

<TableLayout
   xmlns:android=”http://schemas.android.com/apk/res/android”
   android:layout_height=”fill_parent”
   android:layout_width=”fill_parent” >
   
   <TableRow>
      <TextView
         android:text=”User Name:”
         android:width =”120dp”
         />
      
      <EditText
         android:id=”@+id/txtUserName”
         android:width=”200dp” />
   </TableRow>
   
</TableLayout>

相对布局

RelativeLayout 相对布局使您能够指定子视图如何相对于彼此定位。它可以这样声明。

<RelativeLayout
   android:id=”@+id/RLayout”
   android:layout_width=”fill_parent”
   android:layout_height=”fill_parent”
   xmlns:android=”http://schemas.android.com/apk/res/android” >
</RelativeLayout>

框架布局

FrameLayout 框架布局是屏幕上的占位符,可用于显示单个视图。 可以这样声明。

<?xml version=”1.0” encoding=”utf-8”?>
<FrameLayout
   android:layout_width=”wrap_content”
   android:layout_height=”wrap_content”
   android:layout_alignLeft=”@+id/lblComments”
   android:layout_below=”@+id/lblComments”
   android:layout_centerHorizontal=”true” >
   
   <ImageView
      android:src = “@drawable/droid”
      android:layout_width=”wrap_content”
      android:layout_height=”wrap_content” />
</FrameLayout>

除了这些属性之外,还有其他属性在所有视图和 ViewGroup 中都是通用的。 它们在下面列出 −

序号 视图 & 描述
1

layout_width

指定 View 或 ViewGroup 的宽度

2

layout_height

指定 View 或 ViewGroup 的高度

3

layout_marginTop

指定 View 或 ViewGroup 顶部的额外空间

4

layout_marginBottom

指定 View 或 ViewGroup 底部的额外空间

5

layout_marginLeft

指定 View 或 ViewGroup 左侧的额外空间

6

layout_marginRight

指定 View 或 ViewGroup 右侧的额外空间

7

layout_gravity

指定子视图的定位方式

8

layout_weight

指定布局中应将多少额外空间分配给视图


计量单位

当您在 Android UI 上指定元素的大小时,您应该记住以下度量单位。

序号 计量单位 & 描述
1

dp

与密度无关的像素。 1 dp 相当于 160 dpi 屏幕上的一个像素。

2

sp

与比例无关的像素。 这与 dp 类似,建议用于指定字体大小

3

pt

点,根据物理屏幕尺寸,一个 pt 被定义为 1/72 英寸。

4

px

像素。 对应屏幕上的实际像素


屏幕密度

序号 Density & DPI
1

低密度 (ldpi)

120 dpi

2

中密度 (mdpi)

160 dpi

3

高密度(hdpi)

240 dpi

4

超高密度 (xhdpi)

320 dpi


优化布局

以下是一些创建高效布局的指南。

  • 避免不必要的嵌套
  • 避免使用过多的视图
  • 避免深度嵌套