android之LinearLayout

本文转自: android之LinearLayout

LinearLayout

可以让它的子元素垂直或水平的方式排成一行(不设置方向的时候默认按照垂直方向排列)。
下面示例是在别人基础上修改的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:控制各个组件在布局中的相对大小


[html]  view plain  copy
  1.   
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值