线性和相对布局的总结

android的布局属性参考资料:
http://blog.csdn.net/webrobot/article/details/7425078 

线性布局笔记如下:

android:layout_marginTop属性
指的是该View或者布局(如线性布局)的上方的空间.
而不要错误地理解为是距离父元素顶部的距离(除非它是此父元素的第一个控件).
但是在相对布局中不是这样的,在相对布局中指的是就是距离其父元素的顶部的距离!!!(这句错误,见下)
错误分析:
在线性布局中因为总是垂直或者水平的.它是有序的.所以这个margin就是相对于父控件而言的.
但在相对布局中默认:所以控件都是从一个位置叠加的.所以需要指定某个控件距离顶部的距离.
但是如果A控件通过android:layout_below指定了其在B控件之下,那么这里的layout_marginTop
指的就是A距离B的距离,而不是距离父控件顶部的距离.关键还是在于要深刻理解,相对布局中
"相对"二字

android:layout_gravity属性
指定子View相对于父容器如何摆放

android:gravity属性
指定本元素或者其所有子元素的重力方向(即如何布局)!!
比如指定某个Button中的文字在底部.可采用android:gravity="bottom"
比如指定某个布局下的所有子元素都靠左边可采用android:gravity="left"


LinerLayout的gravity属性描述:
http://www.cnblogs.com/olvo/archive/2012/05/04/2482440.html
这个东西在布局文件中敲一下即可看出来

注意
1 android:layout_gravity和android:layout_weight
  只能当子View位于LinearLayout或者TableLayout里才能应用!!!
2 android:gravity在线性布局和相对布局中都可以使用!!!!


LinearLayout中android:layout_gravity不起作用的例子及分析:
参考资料:
http://www.cnblogs.com/olvo/archive/2012/05/21/2511632.html
注意细节:
因为LinearLayout默认的是:android:orientation="horizontal"
得出结论:
当作为父layout的LinearLayout的属性为android:orientation="vertical"时,android:layout_gravity="?"
此处设为横向的时候才能生效.比如:left,right,center_horizontal等

当作为父layout的LinearLayout的属性为android:orientation="horizental"时,android:layout_gravity="?"
此处设为纵向的时候才能生效.比如top,bottom,center_vertical等

当然也可以这么做:在LinearLayout设置 android:gravity="right" android:orientation="horizontal"
然后在子View中设置android:layout_gravity="right"可以起到作用(废话)

原因分析:
因为android:orientation规定了整个布局的大方向,是从左到右还是至上而下.
比如android:orientation="vertical"那么在大体上就该按照至上而下的方式来.不能随意更改子View的纵向布局.
只可以在一定距离的纵向范围内,横向改变其位置.android:orientation="horizontal"时,原理类似

所以,在线性布局中android:layout_gravity="bottom|right" 不可以将控件放在右下角.
可以使用相对布局来实现:
android:layout_alignParentRight="true"
android:layout_alignParentBottom="true"
这样就到了右下角

继续在此看一个小问题:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:gravity="right"
    android:orientation="horizontal" >

    <Button
        android:id="@+id/button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginRight="20dip"
        android:layout_gravity="right"
        android:text="Button1" />

    <Button
        android:id="@+id/button2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginRight="10dip"
        android:text="Button2" />
   
</LinearLayout>


 

为什么Button1没有在最右边.而是Button2在最右边.Button1在Button2的左边?
1 方向是horizontal也就是说从左到右排列的.
2 android:gravity="right"即所有的控件都靠右边.
  于是首先Button1靠了右边,但是紧接着Button2来了它也靠右.挤开了Button1

 

 

相对布局笔记如下:
重要参考资料:
http://blog.csdn.net/johnny901114/article/details/7865617  此资料不错
http://blog.csdn.net/huangxy10/article/details/8181082  相对和线性布局的对比 很不错
但是该资料有错误的地方:
android:layout_gravity和android:layout_weight
只能当子View位于LinearLayout或者TableLayout里才能应用!!!!!
这个对比给出一个结论:就是说某个属性在该控件中到底能否起到作用,还是要看其父布局是什么.


结论举例1,比如:
在一个RelativeLayout下有一个TextView控件,那么它就有android:layout_centerInParent=""属性
但是在LinerLayout下TextView控件就没有android:layout_centerInParent=""属性!!!!!!!


结论举例2,比如:
线性布局根本没有这么几个对齐方式:
android:layout_centerHorizontal   为true,将该控件的置于水平居中;
android:layout_centerVertical     为true,将该控件的置于垂直居中;
android:layout_centerInParent     为true,将该控件的置于父控件的中央(同时设置水平和垂直居中);
但是当某线性布局的的父元素是相对布局,那么它也是具有这些属性!!!!

小结:
1 相对布局中不存在属性android:orientation,但是如果写了也是不会报错的.
2 在RelativeLayout布局中居中是采用android:layout_centerxxx来实现的.
  而在LinerLayout布局中居中是采用 android:gravity来实现的.


有这么一种情况
在相对布局中,控件的布局如下:

 <ImageButton
            android:id="@+id/report_left"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentLeft="true"
            android:layout_centerVertical="true"
            android:layout_marginLeft="5dp"
            android:background="@drawable/tanteimodoru"
            android:padding="5dp"
 />



注意:
同时设置了android:layout_alignParentLeft="true"和 android:layout_marginLeft="5dp"
也就是说相对于父控件的左边对齐,同时设置了layout_marginLeft.
所以ImageButton不会真的靠左对齐,存在5dp的距离

 


 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

谷哥的小弟

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

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

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

打赏作者

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

抵扣说明:

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

余额充值