LinearLayout(线性布局)

简介:

要点:

android:orientation="vertical"垂直线性布局,"horizontal"水平线性布局

android:gravity="top"(buttom、left、right、center_vertical、fill_vertical、center_horizontal、fill_horizontal、center、fill、clip_vertical、clip_horizontal)控制布局中控件的对齐方式。如果是没有子控件的控件设置此属性,表示其内容的对齐方式,比如说TextView里面文字的对齐方式;若是有子控件的控件设置此属性,则表示其子控件的对齐方式,gravity如果需要设置多个属性值,需要使用“|”进行组合

android:gravity 与 android:layout_gravity的区别
android:gravity是指定本元素的子元素相对它的对齐方式。
android:layout_gravity是指定本元素相对它的父元素的对齐方式。

 android:layout_weight="1"通过设置控件的layout_weight属性以控制各个控件在布局中的相对大小,线性布局会根据该控件layout_weight值与其所处布局中所有控件layout_weight值之和的比值为该控件分配占用的区域。在水平布局的LinearLayout中有两个Button,这两个Button的layout_weight属性值都为1,那么这两个按钮都会被拉伸到整个屏幕宽度的一半。如果layout_weight指为0,控件会按原大小显示,不会被拉伸;对于其余layout_weight属性值大于0的控件,系统将会减去layout_weight属性值为0的控件的宽度或者高度,再用剩余的宽度或高度按相应的比例来分配每一个控件显示的宽度或高度。

例:

布局代码:

复制代码
 1 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
 2     xmlns:tools="http://schemas.android.com/tools"
 3     android:layout_width="match_parent"
 4     android:layout_height="match_parent"
 5     android:orientation="vertical"
 6     tools:context=".LinearLayoutActivity" >
 7 
 8     <LinearLayout
 9         android:layout_width="match_parent"
10         android:layout_height="match_parent"
11         android:layout_weight="1"
12         android:orientation="horizontal" >
13 
14         <Button
15             android:layout_width="wrap_content"
16             android:layout_height="match_parent"
17             android:layout_weight="1"
18             android:background="#aa0000"
19             android:gravity="center_horizontal|center_vertical"
20             android:text="第一列"
21             android:textSize="15sp" >
22         </Button>
23 
24         <Button
25             android:layout_width="wrap_content"
26             android:layout_height="match_parent"
27             android:layout_weight="1"
28             android:background="#00aa00"
29             android:gravity="center_horizontal"
30             android:text="第二列"
31             android:textSize="15sp" >
32         </Button>
33 
34         <Button
35             android:layout_width="wrap_content"
36             android:layout_height="match_parent"
37             android:layout_weight="1"
38             android:background="#0000aa"
39             android:gravity="center|bottom"
40             android:text="第三列"
41             android:textSize="15sp" >
42         </Button>
43 
44         <Button
45             android:layout_width="wrap_content"
46             android:layout_height="match_parent"
47             android:layout_weight="1"
48             android:background="#aaaa00"
49             android:gravity="bottom"
50             android:text="第四列"
51             android:textSize="15sp" >
52         </Button>
53     </LinearLayout>
54 
55     <LinearLayout
56         android:layout_width="match_parent"
57         android:layout_height="match_parent"
58         android:layout_weight="1"
59         android:orientation="vertical" >
60 
61         <Button
62             android:layout_width="match_parent"
63             android:layout_height="match_parent"
64             android:layout_weight="1"
65             android:gravity="bottom"
66             android:text="第1行"
67             android:textSize="15sp" >
68         </Button>
69 
70         <Button
71             android:layout_width="match_parent"
72             android:layout_height="match_parent"
73             android:layout_weight="1"
74             android:gravity="bottom"
75             android:text="第2行"
76             android:textSize="15sp" >
77         </Button>
78 
79         <Button
80             android:layout_width="match_parent"
81             android:layout_height="match_parent"
82             android:layout_weight="1"
83             android:gravity="bottom"
84             android:text="第3行"
85             android:textSize="15sp" >
86         </Button>
87 
88         <Button
89             android:layout_width="match_parent"
90             android:layout_height="match_parent"
91             android:layout_weight="1"
92             android:gravity="bottom"
93             android:text="第4行"
94             android:textSize="15sp" >
95         </Button>
96     </LinearLayout>
97 
98 </LinearLayout>
复制代码

 其它干货下载资源已放入微信公众号【一个码农的日常】

本文转自欢醉博客园博客,原文链接http://www.cnblogs.com/zhangs1986/archive/2013/01/17/2864237.html如需转载请自行联系原作者


欢醉

 

相关文章
|
算法 Java Android开发
LinearLayout(线性布局)
本节开始讲Android中的布局,今天我们要讲解的就是第一个布局,LinearLayout(线性布局),我们屏幕适配的使用用的比较多的就是LinearLayout的weight(权重属性),在这一节里,我们会详细地解析LinearLayout,包括一些基本的属性,Weight属性的使用,以及比例如何计算,另外还会说下一个用的比较少的属性:android:divider绘制下划线!
50 0
|
容器
RelativeLayout(相对布局)
LinearLayout也是我们用的比较多的一个布局,我们更多的时候更钟情于他的weight(权重)属性,等比例划分,对屏幕适配还是帮助蛮大的;但是使用LinearLayout的时候也有一个问题,就是当界面比较复杂的时候,需要嵌套多层的LinearLayout,这样就会降低UI Render的效率(渲染速度),而且如果是listview或者GridView上的item,效率会更低,另外太多层LinearLayout嵌套会占用更多的系统资源,还有可能引发stackoverflow;但是如果我们使用RelativeLayout的话,可能仅仅需要一层就可以完成了,以父容器或者兄弟组件参考+margi
62 0
|
XML Android开发 数据格式
Android开发中RelativeLayout相对布局
Android开发中RelativeLayout相对布局
288 0
Android开发中RelativeLayout相对布局
|
XML Android开发 数据格式
|
XML Android开发 数据格式
Android基础_2 Activity线性布局和表格布局
在activity的布局中,线性布局和表格布局是最简单的,这次分别从线性布局,表格布局以及线性布局和表格混合布局做了实验,实验中只需要编写 相应的xml的代码,java代码不需要更改,因为我们这里只是练习android的界面设计。
1278 0

热门文章

最新文章