android开发 视图联动_android开发_ViewGroup(组视图)-- 五大布局

view组--ViewGroup(组视图)

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

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

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

(3) FrameLayout 帧布局

(4) AbsoluteLayout 绝对布局

(5) TableLayout  表格布局

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

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

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

1

5

6

7

8 android:layout_width="match_parent"

9 android:layout_height="match_parent"

10 android:orientation="horizontal" >

11

12

13

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

4

5 android:layout_width="match_parent"

6 android:layout_height="match_parent"

7 android:orientation="horizontal" >

8

9

10 android:id="@+id/textView1"

11 android:layout_width="wrap_content"

12 android:layout_height="wrap_content"

13 android:text="TextView" />

14

15

16 android:id="@+id/textView2"

17 android:layout_width="wrap_content"

18 android:layout_height="wrap_content"

19 android:text="TextView" />

20

21

22 android:id="@+id/textView3"

23 android:layout_width="wrap_content"

24 android:layout_height="wrap_content"

25 android:text="TextView" />

26

27

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

1 <?xml version="1.0" encoding="utf-8"?>

2

3

4

5 android:layout_width="match_parent"

6 android:layout_height="match_parent"

7 android:orientation="horizontal" >

8

9

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

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

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

纵向布局同理。

例2:实现下面布局

颜色值忽略

1 <?xml version="1.0" encoding="utf-8"?>

2

3 android:layout_width="match_parent"

4 android:layout_height="match_parent"

5 android:orientation="vertical" >

6

8 android:layout_height="0dp"

9 android:layout_weight="1"

10 android:background="#F00">

11

12

14 android:layout_height="match_parent"

15 android:layout_weight="1"

16 android:background="#F00"

17 />

18

20 android:layout_height="match_parent"

21 android:layout_weight="1"

22 android:background="#0F0"

23 />

24

26 android:layout_height="match_parent"

27 android:layout_weight="1"

28 android:background="#00F"

29 />

30

31

33 android:layout_height="0dp"

34 android:layout_weight="1"

35 android:orientation="vertical" >

36

38 android:layout_height="0dp"

39 android:layout_weight="1"

40 android:background="#F00"

41 />

42

44 android:layout_height="0dp"

45 android:layout_weight="1"

46 android:background="#0F0"

47 />

48

49

51 android:layout_height="0dp"

52 android:layout_weight="1"

53 android:background="#00F">

54

56 android:layout_height="match_parent"

57 android:layout_weight="1"

58 android:background="#F00"

59 />

60

62 android:layout_height="match_parent"

63 android:layout_weight="1"

64 android:background="#0F0"

65 />

66

68 android:layout_height="match_parent"

69 android:layout_weight="1"

70 android:background="#00F"

71 />

72

73

74

View Code

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位置默认在左上角

1 <?xml version="1.0" encoding="utf-8"?>

2

3 android:layout_width="match_parent"

4 android:layout_height="match_parent" >

5

6

7

9 android:layout_width="wrap_content"

10 android:layout_height="wrap_content"

11 android:text="最左上角" />

12

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

android:layout_centerVertical="true"     纵向中间

1 <?xml version="1.0" encoding="utf-8"?>

2

3 android:layout_width="match_parent"

4 android:layout_height="match_parent" >

5

6

8 android:layout_width="wrap_content"

9 android:layout_height="wrap_content"

10 android:layout_centerHorizontal="true"

11 android:layout_centerVertical="true"

12 android:text="两种属性定位到中间" />

13

14

2.1.3属性:android:layout_centerInParent="true" 父窗体中间

1 <?xml version="1.0" encoding="utf-8"?>

2

3 android:layout_width="match_parent"

4 android:layout_height="match_parent" >

5

7 android:layout_width="wrap_content"

8 android:layout_height="wrap_content"

9 android:layout_centerInParent="true"

10 android:text="一种属性定位到中间" />

11

2.1.4属性:android:layout_alignParentLeft="true" 对齐到父窗口的左面,其中align是“对齐”的意思

1 <?xml version="1.0" encoding="utf-8"?>

2

3 android:layout_width="match_parent"

4 android:layout_height="match_parent" >

5

7 android:layout_height="match_parent"

8 android:layout_alignParentLeft="true"

9 android:text="最左面" />

10

11

2.1.5属性:android:layout_alignParentRight="true" 对齐到父窗口的右面,其中align是“对齐”的意思

1 1 <?xml version="1.0" encoding="utf-8"?>

2 2

3 3 android:layout_width="match_parent"

4 4 android:layout_height="match_parent" >

5 5

7 7 android:layout_height="match_parent"

8 8 android:layout_alignParentRight="true"

9 9 android:text="最右面" />

10 1011 11

2.1.6属性:android:layout_alignParentTop="true" 对齐到父窗口的右面,其中align是“对齐”的意思

1 <?xml version="1.0" encoding="utf-8"?>

2

3 android:layout_width="match_parent"

4 android:layout_height="match_parent" >

5

7 android:layout_height="wrap_content"

8 android:layout_alignParentTop="true"

9 android:text="最上面" />

10

2.1.7属性:android:layout_alignParentBottom="true" 对齐到父窗口的右面,其中align是“对齐”的意思

1 <?xml version="1.0" encoding="utf-8"?>

2

3 android:layout_width="match_parent"

4 android:layout_height="match_parent" >

5

7 android:layout_height="wrap_content"

8 android:layout_alignParentBottom="true"

9 android:text="最下面" />

10

11

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"

1 <?xml version="1.0" encoding="utf-8"?>

2

3 android:layout_width="match_parent"

4 android:layout_height="match_parent" >

5

7 android:layout_width="wrap_content"

8 android:layout_height="wrap_content"

9 android:layout_centerInParent="true"

10 android:text="中间"/>

11

13 android:layout_height="wrap_content"

14 android:layout_above="@id/center_btn"

15 android:layout_alignLeft="@id/center_btn"

16 android:text="相对"/>

17 />18

19

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

4  AbsoluteLayout 绝对布局:

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

表格标签

         行标签

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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值