今天总结下Android开发中有关布局平分的相关技术和实现。


从一个简单的任务入手,“如何在水平方向上一左一右均匀地放置两个Button”,有很多种方式可以实现这个功能,在此做一个简单的总结,顺便深入理解下有关 gravity, layout_weight 等相关概念的原理和应用。


一、效果图


201355141.png


二、思考 RelativeLayout 和 LinearLayout 中分别如何左右放置button


(1) 在RelativeLayout中放置


由于 RelativeLayout 内部的子控件都可以指定相对位置,因此很容易实现左右放置两个Button的任务,如下所示:


<RelativeLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
   android:orientation="horizontal">
   
    <Button
       android:layout_width="wrap_content"
       android:layout_height="wrap_content"
       android:text="Left Button"
        android:layout_alignParentLeft="true"/>
   
    <Button
       android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Right Button"
        android:layout_alignParentRight="true"/>
   
</RelativeLayout>


效果如图所示:


202738817.png


(2) 在LinearLayout中放置


由于LinearLayout是顺序排放所有的控件,如果希望两个button分开地位于一左一右,则必须在两个Button之间再加一个layout_weight为1.0的隐形view才能实现。示例如下:


<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_margin="10dp"
    android:orientation="horizontal">
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                
    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Left Button"/>
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                
    <View
        android:layout_width="0dp"
        android:layout_weight="1.0"
        android:layout_height="0dp"/>
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                
    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Right Button"/>
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                
</LinearLayout>


效果如图所示:


202621615.png


三、 在水平方向“均分”整个Layout


其实,对于LinearLayout而言,最简单地平分方式就是将子控件的layout_width属性值设置为0,同时layout_weight属性设置为1.0这样,所有的子控件就会自动均分整个LinearLayout ,例如:


<LinearLayout
    android:layout_width="match_parent"
   android:layout_height="wrap_content"
   android:layout_margin="10dp"
   android:orientation="horizontal">
   
   <Button
       android:layout_width="0dp"
       android:layout_height="wrap_content"
       android:layout_weight="1.0"
       android:text="Left Button"/>
   
    <Button
       android:layout_width="0dp"
       android:layout_height="wrap_content"
       android:layout_weight="1.0"     
        android:text="Right"/>
   
</LinearLayout>


效果如图:

203157172.png


由图中效果可以看到,两个Button很好地一左一右平分了整个LinearLayout,但是由于Button的layout_width设置的是0dp,通过layout_weight来决定长度,因此Button都被横向拉长了,如果希望Button的layout_width设置为wrap_content,那该如何实现均分呢?其实,可以通过在Button外再包围一层LinearLayout来实现,示例如下:


<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_margin="10dp"
    android:orientation="horizontal">   
                                                                                                                                                                                                                                                                                                                                  
    <LinearLayout
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1.0"
        android:orientation="horizontal">       
                                                                                                                                                                                                                                                                                                                                  
        <Button
        android:layout_width="wrap_content"
            android:layout_height="wrap_content"       
            android:text="Left Button"
            android:layout_gravity="center"/>
    </LinearLayout>
                                                                                                                                                                                                                                                                                                                                  
    <LinearLayout
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1.0"
        android:orientation="horizontal"
        android:gravity="center_horizontal">
                                                                                                                                                                                                                                                                                                                                  
        <Button
        android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Right Button"/>
    </LinearLayout>
</LinearLayout>


这样地话,由两个Button外围的LinearLayout平分了整个水平的layout,两个button的大小采用wrap_content,也不会被拉伸,并且通过外围的LinearLayout的android:gravity属性实现了居中。


这里注意Button外围的LinearLayout的android:gravity属性,很关键,该属性决定其子控件在该控件中的位置。所以Button外围的LinearLayout设置了android:gravity="center",因此Button才能在该LinearLayout的正中央。


思考,如果给Button加一个android:gravity="right"的属性,会是什么效果呢?


答案: 会使得 Button 内部的文字向右靠齐,而不是在中央。


四、小结

关于布局的平分就聊到这里了,其实从这么一个简单的任务需求的实现,还是可以学到很多技巧和知识点的,文中有不清楚的或者错误的地方,欢迎留言讨论或者来信lujun.hust@gmail.com交流,,或者关注我的新浪微博 @卢_俊 获取最新的文章和资讯。