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>

 

时间: 2024-09-02 15:29:00

LinearLayout中margin属性小结的相关文章

Android编程开发之EditText中inputType属性小结

本文总结分析了Android编程开发之EditText中inputType属性.分享给大家供大家参考,具体如下: android 1.5以后添加了软件虚拟键盘的功能,所以在输入提示中将会有对应的软键盘模式. android中inputType属性在EditText输入值时启动的虚拟键盘的风格有着重要的作用.这也大大的方便的操作.有时需要虚拟键盘只为字符或只为数字.所以inputType尤为重要. 复制代码 代码如下:<EditText android:layout_width="fill_

Android开发中LinearLayout布局技巧及layout中drawable属性区别

先介绍drawable属性的区别,这个算是比较简单的,但是还是有一点点的小细节需要进行说明,drawable有五个文件夹,分别为hdpi,ldpi,mdpi,xdpi,xxdpi,这五个文件夹想必大家都知道,其实就是为了适应不同分辨率,由于手机分辨率的不同,因此我们的图片需要适应不同手机的分辨率...hdpi:480x800   mdpi:480x320   ldpi:320x240xdpi:1280x720 xxdpi 1920x1280其实这个数字并不是非常精确的,只是说明每一个阶段都有一个

margin属性详解

margin在中文中我们翻译成外边距或者外补白(本文中引用外边距).他是元素盒模型(box model)的基础属性. 一.margin的基本特性 margin属性包括margin-top,margin-right,margin-bottom,margin-left,margin,可以用来设置box的margin area.属性margin可以用来同时设置box的四边外边距,而其他的margin属性只能设置其自各的外边距. margin属性可以应用于几乎所有的元素,除了表格显示类型(不包括 tab

由浅入深漫谈margin属性

margin在中文中我们翻译成外边距或者外补白(本文中引用外边距).他是元素盒模型(box model)的基础属性. 一.margin的基本特性 margin属性包括margin-top,margin-right,margin-bottom,margin-left,margin,可以用来设置box的margin area.属性margin可以用来同时设置box的四边外边距,而其他的margin属性只能设置其自各的外边距. margin属性可以应用于几乎所有的元素,除了表格显示类型(不包括 tab

网页制作学习:由浅入深详解CSS的margin属性

css|网页|详解 margin在中文中我们翻译成外边距或者外补白(本文中引用外边距).他是元素盒模型(box model)的基础属性. 一.margin的基本特性 margin属性包括margin-top,margin-right,margin-bottom,margin-left,margin,可以用来设置box的margin area.属性margin可以用来同时设置box的四边外边距,而其他的margin属性只能设置其自各的外边距. margin属性可以应用于几乎所有的元素,除了表格显示

浅析CSS的margin属性

margin是css中的一个常用属性,margin有多种省略写法,刚接触的时候不太容易搞懂,因此本文特别记录一下. css手册中对margin属性的说明为: 检索或设置对象四边的外补丁.默认值为 0 0. 如果提供全部四个参数值,将按上-右-下-左的顺序作用于四边.如果只提供一个,将用于全部的四边.如果提供两个, 第一个用于上-下,第二个用于左-右.如果提供三个,第一个用于上,第二个用于左-右,第三个用于下. margin省略数值的写法,基本原则如下: 1.如果没有left值,则使用right代

html-关于CSS中的属性width

问题描述 关于CSS中的属性width 为什么在CSS中定义的width,用FSCapture工具测量时,却发现不吻和,为什么会这样啊 解决方案 看下margin padding等,用IE F12或者Chrome带的开发工具等具体看下. 解决方案二: width属性仅为内容区宽度,不包括padding.border.margin:......答案就在这里:CSS width属性 解决方案三: 你测量这个干什么呀????????? 解决方案四: 这种情况应该是浏览器设置了缩放,你检查一下浏览器设置

Android 布局学习之——LinearLayout的layout_weight属性

 一直对layout_weight属性感到比较困惑,今天学习一下,来深入了解layout_weight属性和它的用法.      定义     首先,看看Android官方文档是怎么说的,毕竟人家才是权威嘛.                官方文档的意思是:                  layout_weight属性用于分配LinearLayout中的的额外空间(extra space).                  如果View不想拉伸的话,layout_weight值设置为0.否

我的Android进阶之旅------&amp;gt;如何在多个LinearLayout中添加分隔线

如果要适合于所有的Android版本,可以在多个LinearLayout放置用于显示分隔线的View.例如,放一个ImageView组件,然后将其背景设为分隔线的颜色或图像,分隔线View的定义代码如下: <ImageView android:layout_width="fill_parent" android:layout_height="1dp" android:background="#ffffff" /> 效果如下: 在And