[java]
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如下:
[html]
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如下:
[html]
<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>