LinearLayout中margin属性小结ZZ 分类: Android开发 2014-05-30 10:56 114人阅读 评论(0) 收藏

Margin:
android:layout_margin:本元素离上下左右间的距离

 

android:layout_marginStart:本元素离开始的位置的距离

android:layout_marginEnd:本元素离结束位置的距离

 

android:layout_marginBottom

android:layout_marginTop

android:layout_marginLeft

android:layout_marginRight

eg:

    android:orientation="vertical"
    android:layout_height="match_parent"
    android:layout_width="match_parent">
    
   
        android:layout_width="800dp"
        android:layout_height="300dp"
        android:layout_margin="100dp"
        android:orientation="vertical"
        android:background="#ff0000" >       
       
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        
        android:layout_marginTop="200dp"
        android:layout_marginBottom="50dp"
        android:layout_marginLeft="200dp"
        android:layout_marginRight="50dp"
        android:background="#00ff00"        
        />       
   
   
        android:layout_width="100dp"
        android:layout_height="100dp"
        
        android:layout_marginStart="-50dp"
        android:layout_marginEnd="50dp"      
        android:background="#0000ff"
        
        /> 

mainActivity如下:

  1. package com.c;  
  2.   
  3. import android.os.Bundle;  
  4. import android.app.Activity;  
  5.   
  6. public class MainActivity extends Activity  
  7.   
  8.     @Override  
  9.     protected void onCreate(Bundle savedInstanceState)  
  10.         super.onCreate(savedInstanceState);  
  11.      
  12.     public void testHorizontal(){  
  13. //        
  14. //      xmlns:android="http://schemas.android.com/apk/res/android"  
  15. //      android:layout_width="match_parent"  
  16. //      android:layout_height="match_parent"  
  17. //      android:orientation="horizontal"  
  18. //      >  
  19. //  
  20. //      
  21. //          android:layout_width="wrap_content"  
  22. //          android:layout_height="wrap_content"  
  23. //          android:layout_marginLeft="20dip"  
  24. //          android:layout_marginRight="10dip"  
  25. //          android:text="button1" />  
  26. //      
  27. //          android:layout_width="wrap_content"  
  28. //          android:layout_height="wrap_content"  
  29. //          android:layout_marginLeft="20dip"  
  30. //          android:layout_marginRight="20dip"  
  31. //          android:text="button2" />  
  32. //        
  33. //          android:layout_width="wrap_content"  
  34. //          android:layout_height="wrap_content"  
  35. //          android:layout_marginLeft="20dip"  
  36. //          android:layout_marginRight="10dip"  
  37. //          android:text="button3" />  
  38. //  
  39. //    
  40.           
  41.   
  42.      
  43.   
  44.  

** 

 * 当第一种情况: 

 * 线性布局采用的是horizontal 

 * 总结1: 

 * 此时我们水平放置三个Button.其中给第二个 

 * Button2设置android:layout_marginRight="50dip" 

 * 请注意!!!!!!!!!!!!!!!!!! 

 * 此时并不是说明Button2距离父控件的最右边距离为50dip 

 * 而是说明Button2距离它右边的同级子控件的距离为50dip. 

 * (只有其右边没有同级子控件的时候,设置layout_marginRight 

 * 才是相对于父控件的最右边而言的). 

 * 这也就是说当放置Button3的时候是从距离Button2右边50dip的距离开始的. 

 * 总结2: 

 * button1设置了android:layout_marginRight="10dip" 

 * button2设置了android:layout_marginLeft="20dip" 

 * 那么两者的实际间距为10+20=30; 

 * 总结3: 

 * 最后一个子控件存在这么一种情况: 

 * 参见上面的布局代码若设置button3中 

 * android:layout_marginRight="60dip" 

 * 那么button3会被挤变形. 

 * 从以下几种情况: 

 * (0)若只给button3设置android:layout_marginLeft="10dip" 

   android:layout_marginRight="10dip",则正常显示 

 * (1)若只给button3设置android:layout_marginLeft="80dip" 

 *  那么button会变形 

 * (2)若只给button3设置android:layout_marginLeft="80dip" 

 *  android:layout_marginRight="10dip"那么button3会更加 

 *  严重变形,且距离右边的边框有10dip的距离 

 *  可总结出: 

 *  在水平线性布局中最后一个子控件的右边缘距离父控件右边的距离为A 

 *  若给该控件设置android:layout_marginRight="B" 

 *  第一种情况:A>B时控件不会变形 

 *  第二种情况:A

 *  左基准线不会动摇),迫使距离父控件的距离为B. 

 *   

 *  线性垂直布局与此类似 

horizontal.xml如下:

  1. <</span>LinearLayout   
  2.     xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     android:layout_width="match_parent"  
  4.     android:layout_height="match_parent"  
  5.     android:orientation="horizontal"  
  6.     >  
  7.     <</span>Button  
  8.         android:layout_width="wrap_content"  
  9.         android:layout_height="wrap_content"  
  10.         android:layout_marginLeft="20dip"  
  11.         android:layout_marginRight="10dip"  
  12.         android:text="button1" />  
  13.     <</span>Button  
  14.         android:layout_width="wrap_content"  
  15.         android:layout_height="wrap_content"  
  16.         android:layout_marginLeft="10dip"  
  17.         android:layout_marginRight="20dip"  
  18.         android:text="button2" />  
  19.       <</span>Button  
  20.         android:layout_width="wrap_content"  
  21.         android:layout_height="wrap_content"  
  22.         android:layout_marginLeft="10dip"  
  23.         android:layout_marginRight="10dip"  
  24.         android:text="button3" />  
  25.   
  26. </</span>LinearLayout>  


vertical.xml如下:

  1. <</span>LinearLayout   
  2.     xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     android:layout_width="match_parent"  
  4.     android:layout_height="match_parent"  
  5.     android:orientation="vertical"  
  6.     >  
  7.   
  8.     <</span>Button  
  9.         android:layout_width="wrap_content"  
  10.         android:layout_height="wrap_content"  
  11.         android:layout_marginTop="30dip"  
  12.         android:layout_marginBottom="130dip"  
  13.         android:text="button1" />  
  14.     <</span>Button  
  15.         android:layout_width="wrap_content"  
  16.         android:layout_height="wrap_content"  
  17.         android:layout_marginTop="60dip"  
  18.         android:text="button2" />  
  19.      <</span>Button  
  20.         android:layout_width="wrap_content"  
  21.         android:layout_height="wrap_content"  
  22.         android:layout_marginTop="40dip"  
  23.         android:layout_marginBottom="10dip"  
  24.         android:text="button3" />  
  25.         
  26.   
  27. </</span>LinearLayout>  

版权声明:本文为博主原创文章,未经博主允许不得转载。

posted @ 2014-05-30 10:56  leansmall  阅读(133)  评论(0编辑  收藏  举报