1.简介

LinearLayout为安卓三大常用布局中的线性布局。其中,线性布局又分为水平线性布局和垂直线性布局。视图如下所示:

 Android开发--LinearLayout的应用        Android开发--LinearLayout的应用         Android开发--LinearLayout的应用    

                             水平布局

  Android开发--LinearLayout的应用         Android开发--LinearLayout的应用

                             垂直布局

2.构建

在安卓布局中,往往需要我们进行嵌套布局,以下图为例

Android开发--LinearLayout的应用       Android开发--LinearLayout的应用      Android开发--LinearLayout的应用     Android开发--LinearLayout的应用    Android开发--LinearLayout的应用

Android开发--LinearLayout的应用       Android开发--LinearLayout的应用

重点如下所示:

2.1 android:orientation="horizontal/ertical":该属性表示布局为水平方式或垂直方式。

2.2 android:layout_weight="A":该属性表示子布局占总布局的大小,所有的子布局数字相加之和Sum(A),子布局数A/Sum(A)即为所占比例。

2.3 android:gravity="start/center/end/bottom":该属性表示在该布局中的部件处于水平方向的开始位置,居中,水平方向的结束位置或者最下端。

 1 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
 2     xmlns:tools="http://schemas.android.com/tools"
 3     android:id="@+id/LinearLayout1"
 4     android:layout_width="match_parent"
 5     android:layout_height="match_parent"
 6     android:orientation="vertical"
 7     android:paddingBottom="@dimen/activity_vertical_margin"
 8     android:paddingLeft="@dimen/activity_horizontal_margin"
 9     android:paddingRight="@dimen/activity_horizontal_margin"
10     android:paddingTop="@dimen/activity_vertical_margin"
11     tools:context="example.linearlayout.Activity1" >
12 
13     <LinearLayout
14         android:layout_width="fill_parent"
15         android:layout_height="wrap_content"
16         android:orientation="horizontal"
17         android:layout_weight="1" >  
18 
19         <Button
20             android:id="@+id/button1"
21             android:layout_width="wrap_content"
22             android:layout_height="wrap_content"
23             android:text="@string/bu1" />
24 
25         <LinearLayout
26             android:layout_width="fill_parent"
27             android:layout_height="wrap_content"
28             android:orientation="vertical"
29             android:gravity="end" >
30 
31             <Button
32                 android:id="@+id/button2"
33                 android:layout_width="wrap_content"
34                 android:layout_height="wrap_content"
35                 android:text="@string/bu2" />
36 
37         </LinearLayout>
38     </LinearLayout>
39 
40     <LinearLayout
41         android:layout_width="match_parent"
42         android:layout_height="wrap_content"
43         android:orientation="vertical" 
44         android:gravity="center"
45         android:layout_weight="1" >
46 
47         <Button
48             android:id="@+id/button3"
49             android:layout_width="wrap_content"
50             android:layout_height="wrap_content"
51             android:text="@string/bu3" />
52 
53     </LinearLayout>
54 
55     <LinearLayout                               
56         android:layout_width="match_parent"
57         android:layout_height="wrap_content"
58         android:orientation="horizontal"
59         android:layout_weight="1" 
60         android:gravity="bottom" >             //它将它的部件置于最下端,于是它的子布局也会至于最下端,如最后一张图所示
61 
62         <Button
63             android:id="@+id/button5"
64             android:layout_width="wrap_content"
65             android:layout_height="wrap_content"
66             android:text="@string/bu5" />
67 
68         <LinearLayout
69             android:layout_width="fill_parent"
70             android:layout_height="wrap_content"         //这里高度调整为自适应,于是它的高度和按钮高度一致
71             android:orientation="vertical" 
72             android:gravity="end">
73 
74             <Button
75                 android:id="@+id/button4"
76                 android:layout_width="wrap_content"
77                 android:layout_height="wrap_content"
78                 android:text="@string/bu4" />
79 
80         </LinearLayout>
81     </LinearLayout>
82 </LinearLayout>

 



 

相关文章:

  • 2021-09-20
  • 2022-12-23
  • 2021-09-21
  • 2021-11-28
  • 2021-07-01
  • 2021-07-20
  • 2022-02-19
  • 2021-07-15
猜你喜欢
  • 2021-11-20
  • 2021-10-03
  • 2021-09-02
  • 2022-03-08
  • 2021-11-20
  • 2022-12-23
  • 2021-10-01
相关资源
相似解决方案