Android学习笔记06-线性布局LinearLayout

线性布局在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源码如下:

  1 Android_LinearLayout实例
  2  <LinearLayout 
  3      xmlns:android="http://schemas.android.com/apk/res/android"
  4      android:orientation="vertical"
  5      android:background="#FFFFFF"
  6      android:layout_width="match_parent"
  7      android:layout_height="match_parent" >
  8      
  9      <!-- 最上面的输入框 -->
 10      <LinearLayout 
 11      android:orientation="horizontal"
 12      android:background="#FFFFFF"
 13      android:layout_width="match_parent"
 14      android:layout_height="wrap_content" >
 15      
 16          <EditText
 17          android:id="@+id/mEditText"
 18          android:inputType="number"
 19          android:layout_width="match_parent"
 20          android:layout_height="wrap_content"    >
 21          </EditText>
 22          
 23      </LinearLayout>
 24      
 25      <!-- 第一排的四个按键  -->
 26      <LinearLayout 
 27      android:orientation="horizontal"
 28      android:background="#FFFFFF"
 29      android:layout_width="match_parent"
 30      android:layout_height="wrap_content"  >
 31      
 32          <Button 
 33          android:id="@+id/mButton_mc"
 34          android:text="@string/mc"
 35          android:layout_weight="1"
 36          android:layout_width="0dip"
 37          android:layout_height="wrap_content"    >            
 38          </Button>
 39          
 40          <Button 
 41          android:id="@+id/mButton_mPlus"
 42          android:text="@string/mPlus"
 43          android:layout_weight="1"
 44          android:layout_width="0dip"
 45          android:layout_height="wrap_content"    >            
 46          </Button>
 47          
 48          <Button 
 49          android:id="@+id/mButton_mMinus"
 50          android:text="@string/mMinus"
 51          android:layout_weight="1"
 52          android:layout_width="0dip"
 53          android:layout_height="wrap_content"    >            
 54          </Button>
 55          
 56          <Button 
 57          android:id="@+id/mButton_mr"
 58          android:text="@string/mr"
 59          android:layout_weight="1"
 60          android:layout_width="0dip"
 61          android:layout_height="wrap_content"    >            
 62          </Button>
 63          
 64      </LinearLayout>
 65      
 66      <!-- 第二排的四个按键  -->
 67      <LinearLayout 
 68      android:orientation="horizontal"
 69      android:background="#FFFFFF"
 70      android:layout_width="match_parent"
 71      android:layout_height="wrap_content"    >
 72          
 73          <Button 
 74          android:id="@+id/mButton_C"
 75          android:text="@string/C"
 76          android:layout_weight="1"
 77          android:layout_width="0dip"
 78          android:layout_height="wrap_content"    >            
 79          </Button>
 80          
 81          <Button 
 82          android:id="@+id/mButton_PlusAndMinusLog"
 83          android:text="@string/PlusAndMinusLog"
 84          android:layout_weight="1"
 85          android:layout_width="0dip"
 86          android:layout_height="wrap_content"    >            
 87          </Button>
 88          
 89          <Button 
 90          android:id="@+id/mButton_DivisionLog"
 91          android:text="@string/DivisionLog"
 92          android:layout_weight="1"
 93          android:layout_width="0dip"
 94          android:layout_height="wrap_content"    >            
 95          </Button>
 96          
 97          <Button 
 98          android:id="@+id/mButton_MultiplicationLog"
 99          android:text="@string/MultiplicationLog"
100          android:layout_weight="1"
101          android:layout_width="0dip"
102          android:layout_height="wrap_content"    >
103          </Button>  
104            
105      </LinearLayout>
106      
107      <!-- 第三排的四个按键  -->
108      <LinearLayout 
109      android:orientation="horizontal"
110      android:background="#FFFFFF"
111      android:layout_width="match_parent"
112      android:layout_height="wrap_content"    >
113              
114          <Button 
115          android:id="@+id/mButton_Number7"
116          android:text="@string/Number7"
117          android:layout_weight="1"
118          android:layout_width="0dip"
119          android:layout_height="wrap_content"    >            
120          </Button>
121          
122          <Button 
123          android:id="@+id/mButton_Number8"
124          android:text="@string/Number8"
125          android:layout_weight="1"
126          android:layout_width="0dip"
127          android:layout_height="wrap_content"    >            
128          </Button>
129          
130          <Button 
131          android:id="@+id/mButton_Number9"
132          android:text="@string/Number9"
133          android:layout_weight="1"
134          android:layout_width="0dip"
135          android:layout_height="wrap_content"    >            
136          </Button>
137          
138          <Button 
139          android:id="@+id/mButton_SubtractionLog"
140          android:text="@string/SubtractionLog"
141          android:layout_weight="1"
142          android:layout_width="0dip"
143          android:layout_height="wrap_content"    >
144          </Button>
145          
146      </LinearLayout>
147      
148      <!-- 第四排的四个按键  -->
149      <LinearLayout 
150      android:orientation="horizontal"
151      android:background="#FFFFFF"
152      android:layout_width="match_parent"
153      android:layout_height="wrap_content"    >
154              
155          <Button 
156          android:id="@+id/mButton_Number4"
157          android:text="@string/Number4"
158          android:layout_weight="1"
159          android:layout_width="0dip"
160          android:layout_height="wrap_content"    >            
161          </Button>
162          
163          <Button 
164          android:id="@+id/mButton_Number5"
165          android:text="@string/Number5"
166          android:layout_weight="1"
167          android:layout_width="0dip"
168          android:layout_height="wrap_content"    >            
169          </Button>
170          
171          <Button 
172          android:id="@+id/mButton_Number6"
173          android:text="@string/Number6"
174          android:layout_weight="1"
175          android:layout_width="0dip"
176          android:layout_height="wrap_content"    >            
177          </Button>
178          
179          <Button 
180          android:id="@+id/mButton_AdditionLog"
181          android:text="@string/AdditionLog"
182          android:layout_weight="1"
183          android:layout_width="0dip"
184          android:layout_height="wrap_content"    >
185          </Button>
186          
187      </LinearLayout>  
188      
189      <!-- 最后两排的六个按键  -->
190      <LinearLayout 
191      android:orientation="horizontal"
192      android:background="#FFFFFF"
193      android:baselineAligned="false"
194      android:layout_width="match_parent"
195      android:layout_height="wrap_content"    >
196       
197          <!-- 右下角等号左边的五个按钮  -->
198          <LinearLayout 
199          android:orientation="vertical"
200          android:background="#FFFFFF"
201          android:layout_weight="3"
202          android:layout_width="0dip"
203          android:layout_height="wrap_content"    >
204          
205              <!-- 左下角的1、2、3三个按钮  -->
206              <LinearLayout 
207              android:orientation="horizontal"
208              android:background="#FFFFFF"
209              android:layout_width="match_parent"
210              android:layout_height="wrap_content"    > 
211              
212                  <Button 
213                  android:id="@+id/mButton_Number1"
214                  android:text="@string/Number1"
215                  android:layout_weight="1"
216                  android:layout_width="0dip"
217                  android:layout_height="wrap_content"    >
218                  </Button>  
219                  
220                  <Button 
221                  android:id="@+id/mButton_Number2"
222                  android:text="@string/Number2"
223                  android:layout_weight="1"
224                  android:layout_width="0dip"
225                  android:layout_height="wrap_content"    >
226                  </Button>
227                  
228                  <Button 
229                  android:id="@+id/mButton_Number3"
230                  android:text="@string/Number3"
231                  android:layout_weight="1"
232                  android:layout_width="0dip"
233                  android:layout_height="wrap_content"    >
234                  </Button>            
235                  
236              </LinearLayout>
237              
238              <!-- 左下角的0和。两个按钮  -->
239              <LinearLayout 
240              android:orientation="horizontal"
241              android:background="#FFFFFF"
242              android:layout_width="match_parent"
243              android:layout_height="wrap_content"    >     
244              
245                  <Button 
246                  android:id="@+id/mButton_Number0"
247                  android:text="@string/Number0"
248                  android:layout_weight="2"
249                  android:layout_width="0dip"
250                  android:layout_height="wrap_content"    >
251                  </Button>        
252                  
253                  <Button 
254                  android:id="@+id/mButton_Point"
255                  android:text="@string/Point"
256                  android:layout_weight="1"
257                  android:layout_width="0dip"
258                  android:layout_height="wrap_content"    >
259                  </Button>
260                  
261              </LinearLayout>  
262                      
263          </LinearLayout> 
264          
265          <!-- 右下角的等号  -->
266          <LinearLayout 
267          android:background="#FFFFFF"
268          android:layout_weight="1"
269          android:layout_width="0dip"
270          android:layout_height="match_parent"    >
271          
272                  <Button 
273                  android:id="@+id/mButton_EqualLog"
274                  android:text="@string/EqualLog"
275                  android:layout_width="match_parent"
276                  android:layout_height="match_parent"    >
277                  </Button>    
278          </LinearLayout> 
279          
280      </LinearLayout> 
281      
282  </LinearLayout>

 

 效果图如图1所示:

图1:Android_LinearLayout实例

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

 1 Android_LinearLayout实例
 2  <resources>
 3    
 4      <string name="app_name">Android_LinearLayout</string>
 5      <string name="hello_world">Hello world!</string>
 6      <string name="menu_settings">Settings</string>
 7      <string name="title_activity_main">MainActivity</string>
 8      
 9      <string name="mc">mc</string>
10      <string name="mPlus">m+</string>
11      <string name="mMinus">m-</string>
12      <string name="mr">mr</string>
13      <string name="C">C</string>
14      <string name="PlusAndMinusLog">+/-</string>    
15      <string name="DivisionLog">/</string>
16      <string name="MultiplicationLog">*</string> 
17      <string name="Number7">7</string>
18      <string name="Number8">8</string>
19      <string name="Number9">9</string>    
20      <string name="SubtractionLog">-</string>      
21      <string name="Number4">4</string>
22      <string name="Number5">5</string>
23      <string name="Number6">6</string>    
24      <string name="AdditionLog">+</string>     
25      <string name="Number1">1</string>
26      <string name="Number2">2</string>
27      <string name="Number3">3</string>    
28      <string name="Number0">0</string>  
29      <string name="Point">.</string>    
30      <string name="EqualLog">=</string>    
31          
32  </resources>

 

posted @ 2013-07-05 09:51  殷源  阅读(238)  评论(0编辑  收藏  举报