view组--ViewGroup(组视图)

ViewGroup的作用:在view中添加子控件。ViewGroup的5个子类,就是五大布局:

(1) LinearLayout  线性布局(常用)

(2) RelativeLayout  相对布局(常用)

(3) FrameLayout 帧布局

(4) AbsoluteLayout 绝对布局

(5) TableLayout  表格布局

1 LinearLayout  线性布局:在该布局下包含的子布局列表为 横向  纵向 排布

1.1 LinearLayout 默认是横向布局,即:从左到右 布局控件

指定布局方向: android:orientation=“ ”

 1 <!-- 指定布局方向的属  性:orientation,
2 属性值:horizontal(横向)
3 vertical(纵向)
4 -->
5
6 <!--横向布局-->
7 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
8 android:layout_width="match_parent"
9 android:layout_height="match_parent"
10 android:orientation="horizontal" >
11
12 <!--纵向布局-->
13 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
14 android:layout_width="match_parent"
15 android:layout_height="match_parent"
16 android:orientation="vertical" >

1.2 权重(只有在子控件中才有的属性)

android:layout_weight=" "

例1:没添加权重属性之前:

 1 <?xml version="1.0" encoding="utf-8"?>
2
3 <!-- 指定布局方向的属相为:orientation,属性值:horizontal(横向)或vertical(纵向) -->
4 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
5 android:layout_width="match_parent"
6 android:layout_height="match_parent"
7 android:orientation="horizontal" >
8
9 <TextView
10 android:id="@+id/textView1"
11 android:layout_width="wrap_content"
12 android:layout_height="wrap_content"
13 android:text="TextView" />
14
15 <TextView
16 android:id="@+id/textView2"
17 android:layout_width="wrap_content"
18 android:layout_height="wrap_content"
19 android:text="TextView" />
20
21 <TextView
22 android:id="@+id/textView3"
23 android:layout_width="wrap_content"
24 android:layout_height="wrap_content"
25 android:text="TextView" />
26
27 </LinearLayout> 

添加权重属性  android:layout_weight=" "  之后

 1 <?xml version="1.0" encoding="utf-8"?>
2
3 <!-- 指定布局方向的属相为:orientation,属性值:horizontal(横向)或vertical(纵向) -->
4 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
5 android:layout_width="match_parent"
6 android:layout_height="match_parent"
7 android:orientation="horizontal" >
8
9 <TextView
10 android:id="@+id/textView1"
11 android:layout_width="wrap_content"
12 android:layout_height="wrap_content"
13 android:layout_weight="1"
14 android:text="TextView" />
15
16 <TextView
17 android:id="@+id/textView2"
18 android:layout_width="wrap_content"
19 android:layout_height="wrap_content"
20 android:layout_weight="1"
21 android:text="TextView" />
22
23 <TextView
24 android:id="@+id/textView3"
25 android:layout_width="wrap_content"
26 android:layout_height="wrap_content"
27 android:layout_weight="1"
28 android:text="TextView" />
29
30 </LinearLayout>

纵向布局同理。

例2:实现下面布局

颜色值忽略

 <?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" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:background="#F00"> <View
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:background="#F00"
/>
<View
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:background="#0F0"
/>
<View
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:background="#00F"
/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:orientation="vertical" >
<View
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:background="#F00"
/>
<View
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:background="#0F0"
/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:background="#00F">
<View
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:background="#F00"
/>
<View
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:background="#0F0"
/>
<View
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:background="#00F"
/>
</LinearLayout> </LinearLayout>

2  RelativeLayout  相对布局:

    2.1相对父控件布局

    (1) android:layout_centerHorizontal   横向居中

(2) android:layout_centerVertical       纵向居中

(3) android:layout_centerInParent      横向纵向居中

跟父控件最左边/最右边/顶部/底部对齐

    (1) android:layout_alignParentLeft

(2) android:layout_alignParentRight

(3) android:layout_alignParentTop

(4) android:layout_alignParentBottom

2.1.1位置默认在左上角

 <?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" >
<!--如果没有位置属性,按钮在左上角 --> <Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="最左上角" />
</RelativeLayout>

2.1.2属性:android:layout_centerHorizontal="true" 横向中间

android:layout_centerVertical="true"     纵向中间

 <?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" > <Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:text="两种属性定位到中间" /> </RelativeLayout>

2.1.3属性:android:layout_centerInParent="true" 父窗体中间
 <?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" >
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:text="一种属性定位到中间" />
</RelativeLayout>

2.1.4属性:android:layout_alignParentLeft="true"  对齐到父窗口的左面,其中align是“对齐”的意思
 <?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" >
<Button
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_alignParentLeft="true"
android:text="最左面" /> </RelativeLayout>

2.1.5属性:android:layout_alignParentRight="true"  对齐到父窗口的右面,其中align是“对齐”的意思
  1 <?xml version="1.0" encoding="utf-8"?>
2 <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
3 android:layout_width="match_parent"
4 android:layout_height="match_parent" >
5 <Button
6 android:layout_width="wrap_content"
7 android:layout_height="match_parent"
8 android:layout_alignParentRight ="true"
9 android:text="最右面" />
10
11 </RelativeLayout>

2.1.6属性:android:layout_alignParentTop="true"  对齐到父窗口的右面,其中align是“对齐”的意思
 <?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" >
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:text="最上面" />
</RelativeLayout>
2.1.7属性:android:layout_alignParentBottom="true"  对齐到父窗口的右面,其中align是“对齐”的意思
 <?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" >
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:text="最下面" /> </RelativeLayout>

 
 

    2.2相对于同等级的控件进行布局

         针对已存在的兄弟控件(在某个控件的左边/右边/上面/下面)

(1)android:layout_toLeftOf

(2)android:layout_toRightOf

(3)android:layout_above

(4)android:layout_below

相对于兄弟控件的边对齐

(1)android:layout_alignTop

(2)android:layout_alignBottom

(3) android:layout_alignLeft

(4)android:layout_alignRight

上述属性的值为@id/相对控件的id。如:android:layout_above="@id/center_btn"

 <?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" >
<Button
android:id="@+id/center_btn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:text="中间"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="@id/center_btn"
android:layout_alignLeft="@id/center_btn"
android:text="相对"/>
/> </RelativeLayout>

3  FrameLayout 帧布局:越写在后面的控件,越展示最前面(最上层)

4  AbsoluteLayout 绝对布局:

5 TableLayout 表格布局:就是一个表格(应用场景:银行表格)

<TableLayout><TableLayout/>表格标签

<TableRow><TableRow/>          行标签

上面代码只给出了身份证那一行的代码

android开发_ViewGroup(组视图)-- 五大布局的更多相关文章

  1. Android开发之详解五大布局

    http://bbs.chinaunix.net/thread-3654213-1-1.html 为了适应各式各样的界面风格,Android系统提供了5种布局,这5种布局分别是: LinearLayo ...

  2. Android开发之玩转FlexboxLayout布局

    在这之前,我曾认真的研究过鸿洋大神的Android 自定义ViewGroup 实战篇 -> 实现FlowLayout,按照大神的思路写出了一个流式布局,所有的东西都是难者不会会者不难,当自己能自 ...

  3. Android开发之自定义视图

    继承View 1.重写onMeasure(int wMeasureSpec,int hMeasureSpec)处理程序,这样可以标明视图尺寸 2.重写onDraw,以便绘制我们自己的自定义视图内 3. ...

  4. android开发------编写用户界面之相对布局

    今天要说的是RelativeLayout.RelativeLayout相对于LinearLayout的主要不同点在于它需要一个参照物. 我们先来看一下官方对这个布局的解释: RelativeLayou ...

  5. android开发------编写用户界面之线性布局(补充知识)

    在前面的文章中 http://www.cnblogs.com/ai-developers/p/android_linearlayout.html 我们看到了布局中有这样一个属性: layout_wei ...

  6. android开发------编写用户界面之线性布局

    一个好的应用程序离不开人性化的用户界面.在学习其他东西之前.理应先学习编写程序的布局(外观) 今天,我们就来学习android的UI布局----LinearLayout. LinearLayout,即 ...

  7. Android开发:组播(多播)与广播

    近期由于需要编写能够使同一局域网中的Android客户端与PC端进行自动匹配通信功能的程序,学习并试验了JAVA组播与广播的内容,记录一些理解如下: 一.组播(多播) 背景知识:组播使用UDP对一定范 ...

  8. android 开发 RecyclerView 横排列列表布局

    1.写一个一竖的自定义布局: <?xml version="1.0" encoding="utf-8"?> <LinearLayout xml ...

  9. Android 开发日常积累

    Android 集合 Android 开源项目分类汇总 扔物线的 HenCoder 高级 Android 教程 hencoder HenCoder:给高级 Android 工程师的进阶手册 Andro ...

随机推荐

  1. 源码编译vim

    目录 获取最新版 vim 源码 1 git仓库clone 2, 源码包下载,里面有各个版本的vim压缩包 vim 配置选项 配置示例 参考文章 tip 获取最新版 vim 源码 1 git仓库clon ...

  2. (转载)python调用shell命令之os 、commands、subprocess

    linux系统下进入python交互式环境: 一.os 模块 1.1.os模块的exec方法簇: python交互界面中: In [1]: import os In [2]: os.exec os.e ...

  3. 转:彻底搞清楚javascript中的require、import和export

    原文地址:彻底搞清楚javascript中的require.import和export   为什么有模块概念 理想情况下,开发者只需要实现核心的业务逻辑,其他都可以加载别人已经写好的模块. 但是,Ja ...

  4. c++ 第一次实验

    实验内容: 2-28:实现一个简单的菜单程序,运行时显示“Menu:A(dd) D(elete) S(ort) Q(uit),Selete one:”提示用户输入.A表示增加,D表示删除, S表示排序 ...

  5. js基础--高阶函数(map,reduce,filter,sort)

    高阶函数 一个函数可以接收另一个函数作为参数,这种函数就称之为高阶函数,编写高阶函数,就是让函数的参数能够接收别的函数. function add (x,y,f){return f(x)+f(y)} ...

  6. 洛谷P4204 [NOI2006]神奇口袋 数论

    正解:数论 解题报告: 传送门 第一次用\(\LaTeX\)和\(markdown\),,,如果出了什么锅麻烦在评论跟我港句QAQ \(1)x_{i}\)可以直接离散 \(2)y_{i}\)的顺序对结 ...

  7. iOS的签名机制

    1.从keychain里“从这证书颁发机构请求证书”,这样就在本地生成了一对公私钥,保存的CertificateSigningRequest就是公钥,私钥保存在本地电脑里. 2.苹果自己有一对固定的公 ...

  8. Python3学习之路~8.5 SocketServer实现多并发

    前面几节我们写的socket都只能实现服务端与一个客户端通信,并不能实现服务端与多客户端同时通信.接下来我们就来学习一下如何实现服务端同时与多个客户端通信,即并发. Socket Server soc ...

  9. 【转】mysql保存图片技术决定:保存二进制文件还是只保存图片相对路径,图片放在硬盘上面?

    最近遇到上面这个问题,一开始我就果断否决了数据库保存图片的策略,主要是太蠢!事实上我的决定是正确的,我仅仅理解为mysql读写性能提高的境界,具体为什么可以提高?很模糊,知道我看到了这里: 大佬做的实 ...

  10. adobe air for ios 例子

    越南soha 平台ios ane http://yun.baidu.com/s/1o65G9XS 里面有oc源码,as库,以及打包生成ane,测试ane 全过程