Android Linearlayout 注意事项

                                                        Android Linearlayout 注意事项

         在Android Linearlayout 使用过程中,android:layout_weight 是主要参数之一,是内部组件按照该占空比划分剩余空间;只有合理的配置android:layout_weight和android:layout_height 或android:layout_width才能达到使用的预期。

        当 Linearlayout参数 android:orientation="horizontal" 时,组件按照android:layout_width参数来布局,当Linearlayout参数 android:orientation="vertical"时,组件android:layout_height来布局。

        组件参数android:layout_height 或android:layout_width参数为 “fill_parent”或“match_parent” 时,其他组件或剩余空间为负数。比如下面的情况

   

    <LinearLayout  
           android:layout_width="match_parent"  
           android:layout_height="wrap_content"  
           android:orientation="horizontal" >  
      
           <TextView  
               android:layout_width="match_parent"  
               android:layout_height="wrap_content"  
               android:layout_weight="1"  
               android:background="@android:color/black"  
               android:text="111"  
               android:textSize="20sp" />  
      
           <TextView  
               android:layout_width="match_parent"  
               android:layout_height="wrap_content"  
               android:layout_weight="2"  
               android:background="@android:color/holo_green_light"  
               android:text="222"  
               android:textSize="20sp" />  
屏幕宽度为L,两个的View的宽度都为L,那么剩余宽度为L-(L+L) = -L, 左边空间为L+(-L)*1/3 = (2/3)L;右边空间为L+(-L)*2/3 =1 /3;如果后面还有组件,组件越多,越到后面,空间就越小。 按照android:layout_weight占比计算后,有些view计算后将没有空间,可能会造成其他组件没有空间而无法布局显示的情况;

      所以按照Google官方推荐,当使用android:layout_weight属性时,将android:layout_weight设为“0dp”或者”wrap_content“。这样android:layout_weight就可以理解为占比进行计算了。空间大小为实际组件的大小加上剩余空间该组件按照layout_weight分得的空间。

      Android:layout_gravity和android:gravity的使用区别:

android:gravity:

用来控制元素在该控件里的显示位置。例如,在一个Botton按钮控件中设置如下两个属性,

android:gravity="left"和android:text="提交",这时Button上的文字“提交”将会位于Button的左部。主要用于控件元素相对控件位置调整;

android:layout_gravity:

用来调整该控件在包含此控件的父控件中的位置。比如在Button按钮控件中设置android:layout_gravity="left"属性时,该Button按钮将位于父控件中界面的左部。

这两个属性可选的值有:top、bottom、left、right、center_vertical、fill_vertical、center_horizontal、fill_horizontal、center、fill、clip_vertical。

一个属性可以包含多个值,需用“|”分开。其含义如下:

top 将对象放在其容器的顶部,不改变其大小.
bottom 将对象放在其容器的底部,不改变其大小.
left 将对象放在其容器的左侧,不改变其大小.
right 将对象放在其容器的右侧,不改变其大小.
center_vertical 将对象纵向居中,不改变其大小. 
垂直对齐方式:垂直方向上居中对齐。
fill_vertical 必要的时候增加对象的纵向大小,以完全充满其容器. 
垂直方向填充
center_horizontal 将对象横向居中,不改变其大小. 
水平对齐方式:水平方向上居中对齐
fill_horizontal 必要的时候增加对象的横向大小,以完全充满其容器. 
水平方向填充
center 将对象横纵居中,不改变其大小.
fill 必要的时候增加对象的横纵向大小,以完全充满其容器.
clip_vertical

附加选项,用于按照容器的边来剪切对象的顶部和/或底部的内容. 剪切基于其纵向对齐设置:顶部对齐时,剪切底部;底部对齐时剪切顶部;除此之外剪切顶部和底部.

垂直方向裁剪

clip_horizontal

附加选项,用于按照容器的边来剪切对象的左侧和/或右侧的内容. 剪切基于其横向对齐设置:左侧对齐时,剪切右侧;右侧对齐时剪切左侧;除此之外剪切左侧和右侧.

水平方向裁剪

当我们采用LinearLayout布局时,有以下特殊情况需要我们注意:

(1)当 android:orientation="vertical"  时, android:layout_gravity只有水平方向的设置才起作用,垂直方向的设置不起作用。即:left,right,center_horizontal 是生效的。

(2)当 android:orientation="horizontal" 时, android:layout_gravity只有垂直方向的设置才起作用,水平方向的设置不起作用。即:top,bottom,center_vertical 是生效的。


padding 与 margin的区别

padding

填充的意思,指的是view中的content与view边缘的距离,元素内部距离view的内边缘大小;

margin

表示的是view的外边缘与其他控件边缘的距离,外面的view无法完全靠近这个view的边界;

margin一般用来描述控件间位置关系,而padding一般描述控件内容和控件的位置关系。






     

猜你喜欢

转载自blog.csdn.net/chengf223/article/details/75604093