LinearLayout中margin属性小结

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>


 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

谷哥的小弟

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值