LinearLayout中margin属性小结

简介: mainActivity如下: package com.c; import android.os.Bundle; import android.

mainActivity如下:

package com.c;

import android.os.Bundle;
import android.app.Activity;
/**
 * Demo描述:
 * 分析LinearLayout中margin属性的作用
 * 备注:
 * MainActivity没有实际用处,只是用来记笔记
 */
public class MainActivity extends Activity {

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
	}
	public void testHorizontal(){
//		<LinearLayout 
//	    xmlns:android="http://schemas.android.com/apk/res/android"
//	    android:layout_width="match_parent"
//	    android:layout_height="match_parent"
//	    android:orientation="horizontal"
//	    >
//
//	    <Button
//	        android:layout_width="wrap_content"
//	        android:layout_height="wrap_content"
//	        android:layout_marginLeft="20dip"
//	        android:layout_marginRight="10dip"
//	        android:text="button1" />
//	    <Button
//	        android:layout_width="wrap_content"
//	        android:layout_height="wrap_content"
//	        android:layout_marginLeft="20dip"
//	        android:layout_marginRight="20dip"
//	        android:text="button2" />
//	      <Button
//	        android:layout_width="wrap_content"
//	        android:layout_height="wrap_content"
//	        android:layout_marginLeft="20dip"
//	        android:layout_marginRight="10dip"
//	        android:text="button3" />
//
//	</LinearLayout>
		
/**
 * 当第一种情况:
 * 线性布局采用的是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时.控件会被压缩,并且是从右往左压缩(即控件的
 *  左基准线不会动摇),迫使距离父控件的距离为B.
 *  
 *  线性垂直布局与此类似
 */
	}

}


horizontal.xml如下:

<LinearLayout 
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="horizontal"
    >
    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginLeft="20dip"
        android:layout_marginRight="10dip"
        android:text="button1" />
    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginLeft="10dip"
        android:layout_marginRight="20dip"
        android:text="button2" />
      <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginLeft="10dip"
        android:layout_marginRight="10dip"
        android:text="button3" />

</LinearLayout>


vertical.xml如下:

<LinearLayout 
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    >

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="30dip"
        android:layout_marginBottom="130dip"
        android:text="button1" />
    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="60dip"
        android:text="button2" />
     <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="40dip"
        android:layout_marginBottom="10dip"
        android:text="button3" />
      

</LinearLayout>


 

相关文章
|
XML 前端开发 Android开发
Android XML 布局基础(四)内外边距(margin、padding)
Android XML 布局基础(四)内外边距(margin、padding)
242 0
|
2月前
|
Android开发
android padding和margin的区别
android padding和margin的区别
21 0
|
4月前
|
XML Android开发 数据格式
Android五大布局对象---FrameLayout,LinearLayout ,Absolute
Android五大布局对象---FrameLayout,LinearLayout ,Absolute
29 1
|
XML Android开发 数据格式
Android ImageView的src和background的区别、padding的使用技巧
Android ImageView的src和background的区别、padding的使用技巧
340 0
|
XML 编解码 Android开发
Android 设置Padding和Margin(动态/静态)
在Android界面开发时,为了布局更加合理好看,很多时候会用上Padding和Margin, padding和margin是什么呢?即内边距和外边距; 某个View指定为padding是针对该View里面的子View距离该View距离而言的,或者是里面的内容距离容器的距离。
|
容器
Center 中的 widthFactor 与 heightFactor 属性
Center 中的 widthFactor 与 heightFactor 属性
182 0
Center 中的 widthFactor 与 heightFactor 属性
CardView使用及属性
CardView使用及属性
208 0
CardView使用及属性
|
Web App开发 测试技术