android布局详解

来源:互联网 发布:js跨域修改iframe样式 编辑:程序博客网 时间:2024/06/06 00:12

android 中布局的重要性不言而喻,一个好的布局能够保持代码的高效性以及整洁性。能够在xml文件中进行的交互尽量在xml文件中进行布局,今天就写写android中的几大布

局也算是这段时间给自己的总结。

android中所有和用户进行交互的控件都是继承于View这个基类,最常见的如:TextView,EditText 等等。说到布局就不能不说到ViewGroup,这是个继承于View的布局和视图

容,官方文档给其的定义为:

A ViewGroup is a special view that can contain other views (called children.) The view group is the base class for layouts and views containers. This 
class also defines the ViewGroup.LayoutParams class which serves as the base class for layouts parameters.

(这是一个能够容纳其他views的特别view,这个view 容器是其他布局和视图容器的基类,这个类也规定了LayoutParams作为基类的布局参数)。

基本上所有的布局类都是继承于这个类,如果现有的布局已经不能够满足于你的需求,你可以继承这个类自定义布局。下面给出布局和ViewGroup之间

继承关系:


                                  

上面的图只是画出了常见的布局之间的继承关系。

下面来讲解线性布局:LinearLayout的相关属性及用法:

1:android:orientation:可选值为 vertical:垂直布局,即:每个控件都是在其相邻控件的上或者下面。Horizontal :水平布局,即:每个控件都在其相邻控件的左或者右边

2:android:Layout_width 可选值为:wrap_content,match_parent, fill_parent。关于三者之间的区别,官方文档是这样介绍的:

fill_parent The view should be as big as its parent (minus padding). This constant is deprecated starting from API Level 8 and is replaced by match_parent
match_parent The view should be as big as its parent (minus padding). Introduced in API Level 8.
wrap_content The view should be only big enough to enclose its content (plus padding). 

其实从Android 2.2开始fill_parent   改名为match_parent   ,从API Level为8开始我们可以直接用match_parent   来代替fill_parent  。

3:android:grivaty ,android_layout_grivaty,其之间的区别为:

android:gravity是对元素本身说的,元素本身的文本显示在什么地方靠着换个属性设置,不过不设置默认是在左侧的。

android:layout_gravity是相对与它的父元素说的,说明元素显示在父元素的什么位置。

其可选属性如下表格所示:

                           
4:android:layout_weight ,android:layout_height

      layout_weight 是线性布局独有的属性,很多人弄不清,为什么有时候设置的值小反而占据的空间大,有时候设置的值大反而占据的空间小呢?在弄清这两个差异首先我们

来了解一下match_parent 和wrap_content的差别。

     match_parent 是此控件在父布局尽量占据较大的空间,wrap_content 是在完全将控件的内容显示出来的前提下,尽量占据父布局较小的位置。所以如果我们将控件的属性

设置为match_parent时,设置layout_weight时,其值越小占据的空间就越大,反之就越小。当我们将控件的属性设为wrap_content时,因为尽量其是占据父布局较小的位置故

值越小其占据的空间也就越小。(所以我们记忆的时候就只要记住layout_weight 的值越小其权重越大,这只是为了记忆的方便,实际情况不是这样的)

     现在我们来说layout_weight 的数学计算方法:

     (1)当控件属性为wrap_content:

          我们设置三个控件,其layout_weigh 为wrap_content ,layout_weight分别为1,2,3. 下面我们来看其所占的比重:

         

     <LinearLayout          android:layout_width="match_parent"         android:layout_height="wrap_content"         android:orientation="horizontal">             <TextView        android:id="@+id/textView1"        android:layout_width="wrap_content"        android:layout_weight="1"        android:layout_height="50dip"        android:background="#ffff0000"        android:text="TextView1" />    <TextView        android:id="@+id/textView2"        android:layout_width="wrap_content"        android:layout_weight="2"        android:layout_height="50dip"        android:background="#ff00ff00"        android:text="TextView2" />         <TextView        android:id="@+id/textView3"        android:layout_width="wrap_content"        android:layout_weight="3"        android:layout_height="50dip"        android:background="#7FFFD4"        android:text="TextView2" />             </LinearLayout>

下面我们来看其效果:


 我们可以看到,三个所占屏幕的宽比分别为:1:2:3 ,也就是当控件属性为wrap_content时,按照正常的理解即: layout_weight 值越大所占的比重越大。

       (2)当控件的属性为match_parent时:

其实我们之前所说的layout_weight值越小权重越大,只是强制记忆而已,实际的计算方法为:

首先我们将上面那个xml文件的控件的属性全改为,match_parent  然后设置layout_weight 分别为 1:2:2,接下来我们来计算其每个控件所占的比重,由于其每个属性都为

match_parent  所以假设这个控件原本所占的空间为一个parent 那么,这个控件所占的空间应为:自身的大小+所占剩余空间的比重。

第一个控件所占的空间的大小为=1p+(1p-3p)1/5=3p/5 这个计算方法为:原有的自身的大小+(原有的屏幕大小1p-3个控件自身所占的大小(3p))*自身所占剩余的比重,结果为

所占据的空间大小,同理可得第二个控件所占大小为=1p+(1p-3p)*2/5=1p/5 ,同理第三个控件所占大小=1p+(1p-3p)*2/5=1p/5。效果图如下:

我们还可以用另外一个比例去验证一下:

当我们设置 三个的layout_weight 分别为1:2:3时:其效果为:

为什么第三个控件没有显示了呢?我们来按照什么给出的计算方法去计算一下:第一个所占空间=1p+(1p-3p)*1/6=2p/3,第二个所占空间为=1p+(1p-3p)*2/6=1p/3

第三个所占空间为:1p+(1p-3p)*3/6=0p 看算出的结果可知最后三个所占的空间比为:2:1:0 故最后一个控件没有显示出来。故验证了上述计算方法的正确性。


 因而:正确的记忆方法为:当属性为wrap_content 其比例是按照正常的比例计算,当为match_parent或fill_parent时,layout_weight 越小其所占剩余比例就越小,通过计算其

比例就越大就是我们上述所说的值越小比例越大。

 参考博客:http://blog.csdn.net/softmanfly/article/details/7636510

 

接下来讲一讲 FramLayout布局的常用属性及技巧:

FrameLayout 是一个比较简单的布局容器,在这个布局中只能放置一个控件,后面所有的控件都会覆盖掉其当前的控件,每个添加的控件都被固定在屏幕的左上角,要想设置

当中控件在布局中的位置就必须通过 android:gravity 的属性进行设置。例如布局消息提醒的界面:

<FrameLayout     android:background="@null"             android:layout_width="match_parent"             android:layout_height="fill_parent"             android:layout_weight="3.5" >     <LinearLayout          android:gravity="bottom|center"                     android:layout_width="match_parent"                     android:layout_height="match_parent" >                    <Button                android:id="@+id/sure"                android:layout_width="match_parent"                android:layout_height="wrap_content"          android:layout_marginRight="10dp"          android:layout_gravity="center"          android:background="@drawable/friendactivity_remind_normal"          android:gravity="center"          android:text="确定"          android:textColor="#FFFFFF"/>          </LinearLayout>                    <LinearLayout               android:gravity="top|right|center"                     android:paddingRight="10.0dip"                     android:layout_width="match_parent"                     android:layout_height="match_parent">                    <TextView                     android:textSize="10.0dip"                     android:textColor="@android:color/white"                     android:gravity="center"                     android:id="@+id/tvTabUnread"                     android:background="@drawable/tab_unread_bg"                     android:visibility="visible"                     android:layout_width="wrap_content"                    android:layout_height="wrap_content"                    android:text="2" />            </LinearLayout></FrameLayout>


 

原创粉丝点击