Android程式fill_parent比例詳解:

來源:互聯網
上載者:User

標籤:ges   src   疑問   就會   androi   沒有   簡單的   linear   and   

相信大家在剛開始學習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沒有出現的原因了。

Android程式fill_parent比例詳解:

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.