展会信息港展会大全

Android学习笔记06:线性布局LinearLayout
来源:互联网   发布日期:2015-10-13 15:22:29   浏览:2793次  

导读:线性布局在xml文件中使用<LinearLayout>来定义。 线性布局可以分为水平和垂直方向的布局,可以通过android:orientation来定义方向,android:orient...

线性布局在xml文件中使用<LinearLayout>来定义。

线性布局可以分为水平和垂直方向的布局,可以通过android:orientation来定义方向,android:orientation=“horizontal”表示水平方向,android:orientation=“vertical”表示垂直方向。

android:layout_width表示控件的宽度,android_layout_height表示控件的高度,其属性值有wrap_content、fill_parent、match_parent三种。其中,wrap_content表示填满父控件的空白,fill_parent表示大小刚好足够显示当前控件里的内容,match_parent与fill_parent作用是相同的。

android:layout_weight表示控件的权重,描述了控件所占的比例有多大。所有的视图都有layout_weight值,其默认为零,表示需要显示多大的视图就占据多大的屏幕空间。若赋一个高于零的值,则将父视图中的可用空间分割,分割大小具体取决于每一个视图的layout_weight值以及该值在当前屏幕布局的整体layout_weight值和在其它视图屏幕布局的layout_weight值中所占的比率而定。

下面是一个使用线性布局的实例。activity_main.xml源码如下:

Android_LinearLayout实例

1 <LinearLayout

2xmlns:android="http://schemas.android.com/apk/res/android"

3android:orientation="vertical"

4android:background="#FFFFFF"

5android:layout_width="match_parent"

6android:layout_height="match_parent" >

7

8<!-- 最上面的输入框 -->

9<LinearLayout

10android:orientation="horizontal"

11android:background="#FFFFFF"

12android:layout_width="match_parent"

13android:layout_height="wrap_content" >

14

15<EditText

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

17android:inputType="number"

18android:layout_width="match_parent"

19android:layout_height="wrap_content">

20</EditText>

21

22</LinearLayout>

23

24<!-- 第一排的四个按键-->

25<LinearLayout

26android:orientation="horizontal"

27android:background="#FFFFFF"

28android:layout_width="match_parent"

29android:layout_height="wrap_content">

30

31<Button

32android:id="@+id/mButton_mc"

33android:text="@string/mc"

34android:layout_weight="1"

35android:layout_width="0dip"

36android:layout_height="wrap_content">

37</Button>

38

39<Button

40android:id="@+id/mButton_mPlus"

41android:text="@string/mPlus"

42android:layout_weight="1"

43android:layout_width="0dip"

44android:layout_height="wrap_content">

45</Button>

46

47<Button

48android:id="@+id/mButton_mMinus"

49android:text="@string/mMinus"

50android:layout_weight="1"

51android:layout_width="0dip"

52android:layout_height="wrap_content">

53</Button>

54

55<Button

56android:id="@+id/mButton_mr"

57android:text="@string/mr"

58android:layout_weight="1"

59android:layout_width="0dip"

60android:layout_height="wrap_content">

61</Button>

62

63</LinearLayout>

64

65<!-- 第二排的四个按键-->

66<LinearLayout

67android:orientation="horizontal"

68android:background="#FFFFFF"

69android:layout_width="match_parent"

70android:layout_height="wrap_content">

71

72<Button

73android:id="@+id/mButton_C"

74android:text="@string/C"

75android:layout_weight="1"

76android:layout_width="0dip"

77android:layout_height="wrap_content">

78</Button>

79

80<Button

81android:id="@+id/mButton_PlusAndMinusLog"

82android:text="@string/PlusAndMinusLog"

83android:layout_weight="1"

84android:layout_width="0dip"

85android:layout_height="wrap_content">

86</Button>

87

88<Button

89android:id="@+id/mButton_DivisionLog"

90android:text="@string/DivisionLog"

91android:layout_weight="1"

92android:layout_width="0dip"

93android:layout_height="wrap_content">

94</Button>

95

96<Button

97android:id="@+id/mButton_MultiplicationLog"

98android:text="@string/MultiplicationLog"

99android:layout_weight="1"

100android:layout_width="0dip"

101android:layout_height="wrap_content">

102</Button>

103

104</LinearLayout>

105

106<!-- 第三排的四个按键-->

107<LinearLayout

108android:orientation="horizontal"

109android:background="#FFFFFF"

110android:layout_width="match_parent"

111android:layout_height="wrap_content">

112

113<Button

114android:id="@+id/mButton_Number7"

115android:text="@string/Number7"

116android:layout_weight="1"

117android:layout_width="0dip"

118android:layout_height="wrap_content">

119</Button>

120

121<Button

122android:id="@+id/mButton_Number8"

123android:text="@string/Number8"

124android:layout_weight="1"

125android:layout_width="0dip"

126android:layout_height="wrap_content">

127</Button>

128

129<Button

130android:id="@+id/mButton_Number9"

131android:text="@string/Number9"

132android:layout_weight="1"

133android:layout_width="0dip"

134android:layout_height="wrap_content">

135</Button>

136

137<Button

138android:id="@+id/mButton_SubtractionLog"

139android:text="@string/SubtractionLog"

140android:layout_weight="1"

141android:layout_width="0dip"

142android:layout_height="wrap_content">

143</Button>

144

145</LinearLayout>

146

147<!-- 第四排的四个按键-->

148<LinearLayout

149android:orientation="horizontal"

150android:background="#FFFFFF"

151android:layout_width="match_parent"

152android:layout_height="wrap_content">

153

154<Button

155android:id="@+id/mButton_Number4"

156android:text="@string/Number4"

157android:layout_weight="1"

158android:layout_width="0dip"

159android:layout_height="wrap_content">

160</Button>

161

162<Button

163android:id="@+id/mButton_Number5"

164android:text="@string/Number5"

165android:layout_weight="1"

166android:layout_width="0dip"

167android:layout_height="wrap_content">

168</Button>

169

170<Button

171android:id="@+id/mButton_Number6"

172android:text="@string/Number6"

173android:layout_weight="1"

174android:layout_width="0dip"

175android:layout_height="wrap_content">

176</Button>

177

178<Button

179android:id="@+id/mButton_AdditionLog"

180android:text="@string/AdditionLog"

181android:layout_weight="1"

182android:layout_width="0dip"

183android:layout_height="wrap_content">

184</Button>

185

186</LinearLayout>

187

188<!-- 最后两排的六个按键-->

189<LinearLayout

190android:orientation="horizontal"

191android:background="#FFFFFF"

192android:baselineAligned="false"

193android:layout_width="match_parent"

194android:layout_height="wrap_content">

195

196<!-- 右下角等号左边的五个按钮-->

197<LinearLayout

198android:orientation="vertical"

199android:background="#FFFFFF"

200android:layout_weight="3"

201android:layout_width="0dip"

202android:layout_height="wrap_content">

203

204<!-- 左下角的1、2、3三个按钮-->

205<LinearLayout

206android:orientation="horizontal"

207android:background="#FFFFFF"

208android:layout_width="match_parent"

209android:layout_height="wrap_content">

210

211<Button

212android:id="@+id/mButton_Number1"

213android:text="@string/Number1"

214android:layout_weight="1"

215android:layout_width="0dip"

216android:layout_height="wrap_content">

217</Button>

218

219<Button

220android:id="@+id/mButton_Number2"

221android:text="@string/Number2"

222android:layout_weight="1"

223android:layout_width="0dip"

224android:layout_height="wrap_content">

225</Button>

226

227<Button

228android:id="@+id/mButton_Number3"

229android:text="@string/Number3"

230android:layout_weight="1"

231android:layout_width="0dip"

232android:layout_height="wrap_content">

233</Button>

234

235</LinearLayout>

236

237<!-- 左下角的0和。两个按钮-->

238<LinearLayout

239android:orientation="horizontal"

240android:background="#FFFFFF"

241android:layout_width="match_parent"

242android:layout_height="wrap_content">

243

244<Button

245android:id="@+id/mButton_Number0"

246android:text="@string/Number0"

247android:layout_weight="2"

248android:layout_width="0dip"

249android:layout_height="wrap_content">

250</Button>

251

252<Button

253android:id="@+id/mButton_Point"

254android:text="@string/Point"

255android:layout_weight="1"

256android:layout_width="0dip"

257android:layout_height="wrap_content">

258</Button>

259

260</LinearLayout>

261

262</LinearLayout>

263

264<!-- 右下角的等号-->

265<LinearLayout

266android:background="#FFFFFF"

267android:layout_weight="1"

268android:layout_width="0dip"

269android:layout_height="match_parent">

270

271<Button

272android:id="@+id/mButton_EqualLog"

273android:text="@string/EqualLog"

274android:layout_width="match_parent"

275android:layout_height="match_parent">

276</Button>

277</LinearLayout>

278

279</LinearLayout>

280

281 </LinearLayout>

效果图如图1所示:

图1:Android_LinearLayout实例

activity_main.xml中的Button控件中的android:text定义了各个按钮所显示的文字,其中使用到的字符串全部都定义在res资源目录下的String.xml文件中,其源码如下:

Android_LinearLayout实例

1 <resources>

2

3<string name="app_name">Android_LinearLayout</string>

4<string name="hello_world">Hello world!</string>

5<string name="menu_settings">Settings</string>

6<string name="title_activity_main">MainActivity</string>

7

8<string name="mc">mc</string>

9<string name="mPlus">m+</string>

10<string name="mMinus">m-</string>

11<string name="mr">mr</string>

12<string name="C">C</string>

13<string name="PlusAndMinusLog">+/-</string>

14<string name="DivisionLog">/</string>

15<string name="MultiplicationLog">*</string>

16<string name="Number7">7</string>

17<string name="Number8">8</string>

18<string name="Number9">9</string>

19<string name="SubtractionLog">-</string>

20<string name="Number4">4</string>

21<string name="Number5">5</string>

22<string name="Number6">6</string>

23<string name="AdditionLog">+</string>

24<string name="Number1">1</string>

25<string name="Number2">2</string>

26<string name="Number3">3</string>

27<string name="Number0">0</string>

28<string name="Point">.</string>

29<string name="EqualLog">=</string>

30

31 </resources>

赞助本站

人工智能实验室

相关热词: android开发 教程

AiLab云推荐
推荐内容
展开

热门栏目HotCates

Copyright © 2010-2024 AiLab Team. 人工智能实验室 版权所有    关于我们 | 联系我们 | 广告服务 | 公司动态 | 免责声明 | 隐私条款 | 工作机会 | 展会港