UI的描述

对于Android应用程序中,所有用户界面元素都是由ViewViewGroup对象构建的。View是绘制在屏幕上能与用户进行交互的一个对象。而对于ViewGroup来说,则是一个用于存放其他ViewViewGroup对象的布局容器!

Android为我们提供了ViewViewGroup的两个子类的集合,提供常用的一些输入控件(比如按钮,图片和文本域等)和各种各样的布局模式(比如线程布局,相对布局,绝对布局,帧布局,表格布局等)。

用户界面布局

在你APP软件上的,用户界面上显示的每一个组件都是使用层次结构ViewViewGroup对象来构成的,比如,每个ViewGroup都是不可见容器,每个ViewGroup视图组用于组织子视图View的容器,而它的子视图View可能是输入一些控件或者在某块区域的小部件UI。如果你有了层次结构树,你可以根据自己的需要,设计出一些布局,但要尽量简单,因为越简单的层次结构最适合性能。

要声明布局,可以在代码中实例化对象并构建,最简单的方法也可以使用xml文件。

<?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" >
<TextView android:id="@+id/text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="TextView" />
<Button android:id="@+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button" />
</LinearLayout>

Android中提供了几个常用布局:

  1. LinearLayout线性布局
  2. RelativeLayout相对布局
  3. FrameLayout帧布局
  4. AbsoluteLayout绝对布局
  5. TableLayout表格布局
  6. GridLayout网格布局

描述一下几个重要的

线性布局:

指子控件以水平或垂直方式排列。

相对布局:

指子控件以控件之间的相对位置或子控件相对于父容器的位置排列。

帧布局:

指所有子控件均放在左上角且后面元素直接覆盖在前面元素之上。

绝对布局:

指子控件通过绝对定位x,y位置来决定其位置摆放。

表格布局:

指以行列的形式放置子控件,每一行是一个TableRow对象或者View对象。

LinearLayout线性布局

常用属性:

  1. id:为该组件添加一个资源id
  2. orientation:布局中的排列方式,有两种方式:

    horizontal水平

    vertical竖直
  3. layout_width:布局的宽度,用wrap_content表示组件的实际宽度,match_parent表示填充父容器
  4. layout_height:布局的长度,用wrap_content表示组件的实际长度,match_parent表示填充父容器
  5. gravity:控制组件所包含的子元素的对齐方式
  6. layout_gravity:控制该组件在父容器里的对齐方式
  7. background:为该组件添加一个背景图片

LinearLayout是一个视图组,可以在一个方向垂直或者水平分布所有子项,用android:orientation属性。

<?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" >
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="输入账号" />
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="输入密码" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="登录" />
</LinearLayout>

RelativeLayout相对布局

RelativeLayout是一个相对布局的视图组,用来显示相对位置的子视图类,在默认情况下,所有子视图对会分布在左上角。

  1. layout_alignParentTop:true,视图的上边界与父级的上边界对齐
  2. layout_centerVertical:true,将子类放置在父类中心
  3. layout_below:将该视图放在资源ID下方
  4. layout_toRightOf:将该视图放在资源ID右边
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<EditText
android:id="@+id/name"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="你的名字" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/name"
android:layout_alignParentRight="true"
android:text="正确" />
</RelativeLayout>

GridView网格布局

GridView其实是一个网格一样的视图组件,是一个ViewGroup的二维视图。用适配器可以将布局进行填充。

ListView列表组件

ListView是一个用于显示列表的可以滚动的视图组,列表项也可以用适配器进行添加内容的。

结语

  • 本文主要讲解 Android精通:View与ViewGroup,LinearLayout线性布局,RelativeLayout相对布局,ListView列表组件

  • 下面我将继续对Java、 Android中的其他知识 深入讲解 ,有兴趣可以继续关注

  • 小礼物走一走 or 点赞

最新文章

  1. Android Studio添加aar
  2. SQL Server中 ldf 文件过大的解决方法
  3. 用Python编写博客导出工具
  4. 线段树——Ultra-QuickSort
  5. sqlserver,sqlite,access数据库链接字符串
  6. Java检查型异常和非检查型异常
  7. Windows Azure功能更新:SQL Server AlwaysOn和Notification Hub 正式商用
  8. 转 -- MVC+EF easyui dataGrid 动态加载分页表格
  9. 基于visual Studio2013解决面试题之0302链表中找倒数k项节点
  10. 使用vue-axios请求geoJson数据报错的问题
  11. 亲测可用的国内maven镜像
  12. android MultiDex multidex原理原理下遇见的N个深坑(二)
  13. java反射机制--reflection
  14. Android为TV端助力 进制互相转换
  15. echarts使用笔记四:双Y轴
  16. php+redis配置
  17. odoo仓库单据产品过滤写法
  18. Android 使用RecyclerView SnapHelper详解
  19. Weekly linux and ConferenceByYear(2002-now)
  20. pandas.read_csv 参数 index_col=0

热门文章

  1. Maven项目中遇到的问题及其解决方案
  2. 关于HashMap和HashTable.md
  3. Ubuntu下解决MySQL自启动,chkconfig list 全部off 情况
  4. redis make编译失败的原因
  5. 大数据入门到精通9-真正得wordcount
  6. jquery即时获取上传文件input file文件名
  7. JavaScript资源网址
  8. php json 写入 mysql 的例子
  9. 对话框改变颜色 宽度沾满屏幕 Dialog
  10. input只允许输入正整数