Android程序fill_parent比例详解:

相信大家在刚开始学习android程序设计时,会有点搞不明白“match_parent”与“fill_parent”之间的比例问题;在此做一个实例大家就明白了!
编译如下代码:

 1     <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
 2     xmlns:tools="http://schemas.android.com/tools"
 3     android:id="@+id/Line"
 4     android:layout_width="match_parent"
 5     android:layout_height="match_parent"
 6     android:orientation="horizontal"
 7     >
 8    <LinearLayout    
 9         android:layout_width="fill_parent"    
10         android:layout_height="fill_parent"    
11         android:background="#00ff00"     
12         android:layout_weight="1"/>    
13        
14         
15     <LinearLayout    
16         android:layout_width="fill_parent"    
17         android:layout_height="fill_parent"    
18         android:background="#ff0000"     
19         android:layout_weight="2"/> 
20     
21     <LinearLayout    
22         android:layout_width="fill_parent"    
23         android:layout_height="fill_parent"    
24         android:background="#0000ff"     
25         android:layout_weight="3"/> 
26         
27 </LinearLayout>

运行结果如下:

   这个时候就会有疑问了,怎么会这样,这比例是2:1吧,那么three去哪了?代码里面明明有 three的啊,还设置了3的,而1和2的比例也不对耶,1:2:3却变成了2:1:0,怎么会这样呢? 答:这里其实没那么简单的,还是需要我们计算的,网上给出的算法有几种,这里就给出笔者 觉得比较容易理解的一种: step 1:个个都是fill_parent,但是屏幕只有一个啦,那么1 - 3 = - 2 fill_parent step 2:依次比例是1/6,2/6,3/6 step 3:先到先得,先分给one,计算: 1 - 2 * (1/6) = 2/3 fill_parent 接着到two,计算: 1 - 2 * (2/6) = 1/3 fill_parent 最后到three,计算 1 - 2 * (3/6) = 0 fill_parent step 4:所以最后的结果是:one占了两份,two占了一份,three什么都木有 以上就是为什么three没有出现的原因了。

posted @ 2017-11-04 19:47  稻草人部落  阅读(3251)  评论(0编辑  收藏  举报