Android开发实践:布局的平分


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


一、效果图


201355141.png


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


(1) 在RelativeLayout中放置


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


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
< 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才能实现。示例如下:


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
< 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 ,例如:


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
< 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来实现,示例如下:


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
< 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 内部的文字向右靠齐,而不是在中央。




本文转自 Jhuster 51CTO博客,原文链接:http://blog.51cto.com/ticktick/1321114,如需转载请自行联系原作者

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值