vLinearLayout类,有点像AWT中的FlowLayout,它们都会将容器里的组件一个挨一个地排列起来,LinearLayout不仅可以控制各组件横向排列,也可以控制组件纵向排列(通过android:orientation属性控制)
v需要注意的是LinearLayout布局不会换行:当组件排到容器尽头时,其余的组件将不会被显示。
 
 

android:gravity
setGravity(int)
设置对齐方式,该属性支持:topbottomleftrightcenter_verticalfill_verticalcenter_horizontalfillhorizontalcenterfillclip_verticalclip_horizontal几个属性值,当设置多个值同时生效时,用竖线|分隔(两边不能有空格)
android:orientation
setOrientation(int)
设置组件的排列方式,该属性支持:horizontal(水平)vertical(垂直)

main.xml:

 

  1. <?xml version="1.0" encoding="utf-8"?> 
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
  3.     android:id="@+id/root" 
  4.     android:layout_width="fill_parent" 
  5.     android:layout_height="fill_parent" 
  6.     android:orientation="vertical" > 
  7.  
  8.  
  9.     <TextView 
  10.         android:id="@+id/show" 
  11.         android:layout_width="fill_parent" 
  12.         android:layout_height="wrap_content" 
  13.         android:background="#00ff00" 
  14.         android:text="@string/hello" /> 
  15.  
  16.     <Button 
  17.         android:id="@+id/ok" 
  18.         android:layout_width="wrap_content" 
  19.         android:layout_height="wrap_content" 
  20.         android:text="测试的安卓按钮" /> 
  21.  
  22. </LinearLayout> 

java文件:

  1. import java.util.Date;  
  2.  
  3. import android.app.Activity;  
  4. import android.os.Bundle;  
  5. import android.view.View;  
  6. import android.widget.Button;  
  7. import android.widget.TextView;  
  8.  
  9. public class AndroidtestActivity01 extends Activity {  
  10.     /** Called when the activity is first created. */ 
  11.     @Override 
  12.     public void onCreate(Bundle savedInstanceState) {  
  13.         super.onCreate(savedInstanceState);  
  14.         setContentView(R.layout.main);//选择布局文件  
  15.           
  16.         Button bn=(Button) findViewById(R.id.ok);  
  17.         bn.setOnClickListener(  
  18.                 new View.OnClickListener(){  
  19.                     public void onClick(View v){  
  20.                         final TextView show=(TextView)findViewById(R.id.show);  
  21.                         show.setText("hello!安卓:"+new Date());  
  22.                     }  
  23.                 }  
  24.         );  
  25.           
  26.     }