android之LinearLayout

来源:互联网 发布:怎么删除软件 编辑:程序博客网 时间:2024/05/16 14:08

转自:http://blog.csdn.net/jzp12/article/details/7590591

LinearLayout

LinearLayout是一种线型的布局方式。LinearLayout布局容器内的组件一个挨着一个地排列起来:不仅可以控制个组件横向排列,也可控制各组件纵向排列。通过orientation属性设置线性排列的方向是垂直(vertical)还是纵向(horizontal)(不设置方向的时候默认按照垂直方向排列)。

下面示例是在别人基础上修改的main.xml:
[html] view plain copy
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     android:layout_width="fill_parent"  
  4.     android:layout_height="fill_parent"  
  5.     android:orientation="vertical" >  
  6.     <LinearLayout    
  7.         android:orientation="horizontal"    
  8.         android:layout_width="fill_parent"    
  9.         android:layout_height="wrap_content"    
  10.         android:baselineAligned="false"  
  11.         android:layout_weight="1"  >   
  12.           
  13.         <LinearLayout     
  14.             android:orientation="horizontal"     
  15.             android:layout_width="wrap_content"     
  16.             android:layout_height="fill_parent"     
  17.             android:layout_weight="1">     
  18.             <TextView      
  19.                 android:text="@string/color_green"      
  20.                 android:textColor="#ff0000"      
  21.                 android:background="#00aa00"      
  22.                 android:layout_width="wrap_content"      
  23.                 android:layout_height="fill_parent"      
  24.                 android:layout_weight="1"/>     
  25.             <TextView      
  26.                 android:text="@string/color_blue"      
  27.                 android:background="#0000aa"      
  28.                 android:layout_width="wrap_content"      
  29.                 android:layout_height="fill_parent"      
  30.                 android:layout_weight="1"/>    
  31.         </LinearLayout>     
  32.         <LinearLayout     
  33.              android:orientation="vertical"     
  34.              android:layout_width="wrap_content"     
  35.              android:layout_height="fill_parent"     
  36.              android:layout_weight="1">     
  37.              <TextView      
  38.                  android:text="@string/color_black"      
  39.                  android:background="#000000"      
  40.                  android:layout_width="fill_parent"      
  41.                  android:layout_height="wrap_content"      
  42.                  android:layout_weight="1"/>    
  43.               <TextView      
  44.                   android:text="@string/color_yellow"      
  45.                   android:background="#aaaa00"      
  46.                   android:layout_width="fill_parent"      
  47.                   android:layout_height="wrap_content"      
  48.                   android:layout_weight="1"/>     
  49.               <TextView      
  50.                   android:text="@string/color_unknown"      
  51.                   android:background="#00aaaa"      
  52.                   android:layout_width="fill_parent"      
  53.                   android:layout_height="wrap_content"      
  54.                   android:layout_weight="1"/>    
  55.          </LinearLayout>    
  56.     </LinearLayout>  
  57.     <LinearLayout    
  58.         android:orientation="vertical"    
  59.         android:layout_width="fill_parent"    
  60.         android:layout_height="wrap_content"    
  61.         android:layout_weight="2">    
  62.         <TextView     
  63.             android:text="@string/color_red"     
  64.             android:gravity="fill_vertical"     
  65.             android:background="#aa0000"     
  66.             android:layout_width="fill_parent"     
  67.             android:layout_height="wrap_content"     
  68.             android:layout_weight="2"/>    
  69.         <TextView     
  70.             android:text="@string/color_white"     
  71.             android:textColor="#ff0000"     
  72.             android:background="#ffffff"     
  73.             android:layout_width="fill_parent"     
  74.             android:layout_height="wrap_content"     
  75.             android:layout_weight="2"/>   
  76.     </LinearLayout>    
  77.   
  78. </LinearLayout>  

string.xml
[html] view plain copy
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <resources>  
  3.   
  4.     <string name="hello">Hello World, TestAbsoluteLayoutActivity!</string>  
  5.     <string name="app_name">TestAbsoluteLayout</string>  
  6.     <string name"color_red">red</string>  
  7.     <string name"color_green">green</string>  
  8.     <string name"color_blue">blue</string>  
  9.     <string name"color_white">white</string>  
  10.     <string name"color_black">black</string>  
  11.     <string name"color_yellow">yellow</string>  
  12.     <string name"color_unknown">unknown</string>  
  13.   
  14. </resources>  

效果图:



常用的属性:

Android:orientation:可以设置布局的方向
android:gravity:用来控制组件的对齐方式
layout_weight:控制各个组件在布局中的相对大小


0 0
原创粉丝点击