Android-线性布局(LinearLayout)

   线性布局:有两种方式指定控件位置,一种是水平线性布局horizontal,一种是垂直线性布局vertical。

  1、 android:gravity 与 android:layout_gravity的区别:
  android:gravity是指定本元素的子元素相对它的对齐方式。
  android:layout_gravity是指定本元素相对它的父元素的对齐方式

  2、android:layout_weight通过设置控件的layout_weight属性控制各个控件在布局中的相对大。

  3、android:layout_marginTop="10dp"使该控件与容器或下一个控件之间间隔10dp。

例子:


  1. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  2.   android:layout_width="match_parent"  
  3.   android:layout_height="match_parent"  
  4.   android:orientation="vertical">
  5. <TextView   android:layout_width="match_parent" 
  6.   android:layout_height="wrap_content"  
  7.   android:layout_weight="1"   
  8.   android:layout_marginTop="10dp"    //使控件与容器相隔10dp
  9.   android:background="#000"/>   
  10.  <TextView   android:layout_width="match_parent"   
  11.   android:layout_height="wrap_content"   
  12.   android:layout_weight="1"    
  13.   android:layout_marginTop="10dp"   //使这个控件与上一个控件相隔10dp
  14.   android:background="#808080"/>   
  15.  <TextView    android:layout_width="match_parent"  
  16.   android:layout_height="wrap_content"   
  17.   android:layout_weight="1"   
  18.   android:layout_marginTop="10dp"   
  19.   android:background="#0000cd"/>   
  20.  <TextView    android:layout_width="match_parent"  
  21.   android:layout_height="wrap_content"    
  22.   android:layout_weight="1"    
  23.   android:layout_marginTop="10dp"   
  24.   android:background="#32cd32"/>  
  25.   <TextView   android:layout_width="match_parent"  
  26.   android:layout_height="wrap_content"     
  27.   android:layout_weight="1"       
  28.   android:layout_marginTop="10dp"    
  29.   android:background="#ff0000"/>
  30. </LinearLayout>
  31. <?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"



  1. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  2.   android:layout_width="fill_parent"  
  3.   android:layout_height="fill_parent"  
  4.   android:orientation="vertical">
  5. <EditText  
  6.   android:layout_width="fill_parent"
    
  7.   android:layout_height="wrap_content"  
  8.   android:hint="To" /> 
  9.  <EditText    android:layout_width="fill_parent"  
  10.   android:layout_height="wrap_content"     
  11.   android:hint="Subject"/>   
  12.  <EditText    android:layout_width="fill_parent"  
  13.   android:layout_height="wrap_content"    
  14.   android:layout_weight="1"    
  15.   android:hint="Message"   
  16.   android:gravity="top"/>  
  17.   <Button   android:layout_width="100dp"  
  18.   android:layout_height="wrap_content"        
  19.   android:gravity="right"    
  20.   android:hint="send"/>
  21. </LinearLayout>
  22. <?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值