LinerLayout讲解.docx

上传人:b****6 文档编号:6049386 上传时间:2023-01-03 格式:DOCX 页数:17 大小:20.90KB
下载 相关 举报
LinerLayout讲解.docx_第1页
第1页 / 共17页
LinerLayout讲解.docx_第2页
第2页 / 共17页
LinerLayout讲解.docx_第3页
第3页 / 共17页
LinerLayout讲解.docx_第4页
第4页 / 共17页
LinerLayout讲解.docx_第5页
第5页 / 共17页
点击查看更多>>
下载资源
资源描述

LinerLayout讲解.docx

《LinerLayout讲解.docx》由会员分享,可在线阅读,更多相关《LinerLayout讲解.docx(17页珍藏版)》请在冰豆网上搜索。

LinerLayout讲解.docx

LinerLayout讲解

从头学Android之Android布局管理:

LinerLayout线性布局

分类:

从头学Android系列2011-10-1217:

175628人阅读评论

(2)收藏举报

androidlayoutcontainersencodingxmlimport

LinerLayout线性布局:

这种布局方式是指在这个里面的控件元素显线性,我们可以通过setOrientation(intorientation)来指定线性布局的显示方式,其值有:

HORIZONTAL(0)、VERTICAL

(1)。

默认为HORIZONTAL。

与之相关的我们也可以在布局文件中通过android:

orientation来指定。

同理,其值也有:

horizontal、vertical

LinearLayout是线性布局控件,它包含的子控件将以横向或竖向的方式排列,按照相对位置来排列所有的widgets或者其他的containers,超过边界时,某些控件将缺失或消失,不能完全显示。

因此垂直方式排列时,每一行只会有一个widget或者是container,而不管他们有多宽,而水平方式排列是将会只有一个行高(高度为最高子控件的高度加上边框高度)。

LinearLayout保持其所包含的widget或者是container之间的间隔以及互相对齐(相对一个控件的右对齐、中间对齐或者左对齐)。

关于layout_weight:

LinearLayout还支持为其包含的widget或者是container指定填充权值。

允许其包含的widget或者是container可以填充屏幕上的剩余空间。

剩余的空间会按这些widgets或者是containers指定的权值比例分配屏幕。

默认的weight值为0,表示按照widgets或者是containers实际大小来显示,若高于0的值,则将Container剩余可用空间分割,分割大小具体取决于每一个widget或者是container的layout_weight及该权值在所有widgets或者是containers中的比例。

例如,如果有三个文本框,前两个文本框的取值一个为2,一个为1,显示第三个文本框后剩余的空间的2/3给权值为2的,1/3大小给权值为1的。

而第三个文本框不会放大,按实际大小来显示。

也就是权值越大,重要度越大,显示时所占的剩余空间越大。

示例1:

[html]viewplaincopyprint?

1

xmlversion="1.0"encoding="utf-8"?

>

2

3

android="

4

5android:

orientation="vertical"android:

layout_width="match_parent"

6

7android:

layout_height="match_parent">

8

9

10

11

id="@+id/txt01"android:

layout_width="fill_parent"

12

13android:

layout_height="wrap_content"android:

layout_weight="1"

14

15android:

text="1111"/>

16

17

18

19

id="@+id/txt02"android:

layout_width="fill_parent"

20

21android:

layout_height="wrap_content"android:

layout_weight="2"

22

23android:

text="2222"/>

24

25

26

27

id="@+id/txt03"android:

layout_width="fill_parent"

28

29android:

layout_height="wrap_content"android:

text="3333"/>

30

31

xmlversion="1.0"encoding="utf-8"?

>

android="

android:

orientation="vertical"android:

layout_width="match_parent"

android:

layout_height="match_parent">

id="@+id/txt01"android:

layout_width="fill_parent"

android:

layout_height="wrap_content"android:

layout_weight="1"

android:

text="1111"/>

id="@+id/txt02"android:

layout_width="fill_parent"

android:

layout_height="wrap_content"android:

layout_weight="2"

android:

text="2222"/>

id="@+id/txt03"android:

layout_width="fill_parent"

android:

layout_height="wrap_content"android:

text="3333"/>

 

 

几个常用的XML属性的详解:

属性名称

相关方法

描述

android:

baselineAligned

setBaselineAligned(booleanbaselineAligned)

是否允许用户调整它内容的基线。

android:

baselineAlignedChildIndex

setBaselineAlignedChildIndex(inti)

是当前LinearLayout与其它View的对齐方式

android:

gravity

setGravity(intgravity)

指定控件中内容的基本内容的对齐方式(本元素里的所有元素的重力方向)。

其值有:

top、bottom、left、right、center_vertical、fill_vertical、center_horizontal、fill_horizontal、center、fill、clip_vertical、clip_horizontal

android:

layout_gravity

是当前元素相对于父元素的重力方向

android:

measureWithLargestChild

当被设置为真时,所有的子控件将被认为是具有重量最小面积最大的子控件

android:

orientation

setOrientation(intorientation)

置它内容的对其方向,有两个可以选择的值:

horizontal和vertical。

分别表示水平排列和垂直排列。

android:

weightSum

在Android里我们可以通过两种方式来设置布局文件,一种是可以通过XML文件来设置布局,这也是官方推荐,另外一种方式就是我们可以通过代码来设置我们的布局模式

方式一:

通过XML文件。

只要在onCreate()方法里通过setContentView()指定布局文件即可

[html]viewplaincopyprint?

32

xmlversion="1.0"encoding="utf-8"?

>

33

34

android="

35

36android:

orientation="vertical"

37

38android:

layout_width="fill_parent"

39

40android:

layout_height="fill_parent">

41

42

43

44

45

46android:

orientation="horizontal"

47

48android:

layout_width="fill_parent"

49

50android:

layout_height="fill_parent"

51

52android:

layout_weight="1">

53

54

55

56android:

text="red"

57

58android:

gravity="center_horizontal"

59

60android:

background="#aa0000"

61

62android:

layout_width="wrap_content"

63

64android:

layout_height="fill_parent"

65

66android:

layout_weight="1"/>

67

68

69

70android:

text="green"

71

72android:

gravity="center_horizontal"

73

74android:

background="#00aa00"

75

76android:

layout_width="wrap_content"

77

78android:

layout_height="fill_parent"

79

80android:

layout_weight="1"/>

81

82

83

84android:

text="blue"

85

86android:

gravity="center_horizontal"

87

88android:

background="#0000aa"

89

90android:

layout_width="wrap_content"

91

92android:

layout_height="fill_parent"

93

94android:

layout_weight="1"/>

95

96

97

98android:

text="yellow"

99

100android:

gravity="center_horizontal"

101

102android:

background="#aaaa00"

103

104android:

layout_width="wrap_content"

105

106android:

layout_height="fill_parent"

107

108android:

layout_weight="1"/>

109

110

111

112

113

114

115

116android:

orientation="vertical"

117

118android:

layout_width="fill_parent"

119

120android:

layout_height="fill_parent"

121

122android:

layout_weight="1">

123

124

125

126android:

text="rowone"

127

128android:

textSize="15pt"

129

130android:

layout_width="fill_parent"

131

132android:

layout_height="wrap_content"

133

134android:

layout_weight="1"/>

135

136

137

138android:

text="rowtwo"

139

140android:

textSize="15pt"

141

142android:

layout_width="fill_parent"

143

144android:

layout_height="wrap_content"

145

146android:

layout_weight="1"/>

147

148

149

150android:

text="rowthree"

151

152android:

textSize="15pt"

153

154android:

layout_width="fill_parent"

155

156android:

layout_height="wrap_content"

157

158android:

layout_weight="1"/>

159

160

161

162android:

text="rowfour"

163

164android:

textSize="15pt"

165

166android:

layout_width="fill_parent"

167

168android:

layout_height="wrap_content"

169

170android:

layout_weight="1"/>

171

172

173

174

175

176

xmlversion="1.0"encoding="utf-8"?

>

android="

android:

orientation="vertical"

android:

layout_width="fill_parent"

android:

layout_height="fill_parent">

android:

orientation="horizontal"

android:

layout_width="fill_parent"

android:

layout_height="fill_parent"

android:

layout_weight="1">

android:

text="red"

android:

gravity="center_horizontal"

android:

background="#aa0000"

android:

layout_width="wrap_content"

android:

layout_height="fill_parent"

android:

layout_weight="1"/>

android:

text="green"

android:

gravity="center_horizontal"

android:

background="#00aa00"

android:

layout_width="wrap_content"

android:

layout_height="fill_parent"

android:

layout_weight="1"/>

android:

text="blue"

android:

gravity="center_horizontal"

android:

background="#0000aa"

android:

layout_width="wrap_content"

android:

layout_height="fill_parent"

android:

layout_weight="1"/>

android:

text="yellow"

android:

gravity="center_horizontal"

android:

background="#aaaa00"

android:

layout_width="wrap_content"

android:

layout_height="fill_parent"

android:

layout_weight="1"/>

android:

orientation="vertical"

android:

layout_width="fill_parent"

android:

layout_height="fill_parent"

android:

layout_weight="1">

android:

text="rowone"

android:

textSize="15pt"

android:

layout_width="fill_parent"

android:

layout_height="wrap_content"

android:

layout_weight="1"/>

android:

text="rowtwo"

android:

textSize="15pt"

android:

layout_width="fill_parent"

android:

layout_height="wrap_content"

android:

layout_weight="1"/>

android:

text="rowthree"

android:

textSize="15pt"

android:

layout_width="fill_parent"

android:

layout_height="wrap_content"

android:

layout_weight="1"/>

android:

text="rowfour"

android:

textSize="15pt"

android:

layout_width="fill_parent"

android:

layout_height="wrap_content"

android:

layout_weight="1"/>

 

方式二:

代码方式

LinerLayout类的常用方法及常量

方法及常量

类型

描述

publicstaticfinalintHORIZONTAL

常量

设置水平对齐

publicstaticfinalintVERTICAL

常量

设置垂直对齐

publicLinerLayout(Contextcontext)

构造方法

创建LinerLayout类的对象

publicvoidaddView(Viewchild,ViewGroup.LayoutParamsparams)

普通方法

增加组组件并且指定布局参数

publicvoidaddView(ViewchildView)

普通方法

增加组件

publicvoidsetOrientation(intorientaiton)

普通方法

设置对齐方式

LinerLayout.LayoutParams用于指定线性布局的参数

类结构图:

java.lang.Object

android.view.ViewGroup.LayoutParams

android.view.ViewGroup.MarginLayoutParams

android.widget.LinearLayout.LayoutParams

常用布局参数:

publicstaticfinalintFILL_PARENT

publicstaticfinalintWRAP_CONTENT

[java]viewplaincopyprint?

177packagecom.jiahui.activity;

178

179

180

181importandroid.app.Activity;

182

183importandroid.content.Intent;

184

185importandroid.os.Bundle;

186

187importandroid.view.View;

188

189importandroid.view.ViewGroup;

190

191importandroid.view.View.OnClickListener;

192

193importandroid.widget.Button;

194

195importandroid.widget.LinearLayout;

196

197importandroid.widget.TextView;

198

199importandroid.widget.LinearLayout.LayoutParams;

200

201

202

203/**

204

205*动态设置布局

206

207*

208

209*@authorAdministrator

210

211*

212

213*/

214

215publicclassDyanmic_Layout_ActivityextendsActivity{

216

217

218

219publicvoidonCreate(BundlesavedInstanceState){

220

221super.onCreate(savedInstanceState);

222

223

224

225//定义线性布局管理器

226

227LinearLayoutlayout=newLinearLayout(this);

228

229

230

231//定义布局管理器的指定宽和高

232

展开阅读全文
相关资源
猜你喜欢
相关搜索

当前位置:首页 > 自然科学

copyright@ 2008-2022 冰豆网网站版权所有

经营许可证编号:鄂ICP备2022015515号-1