android:layout_weight越大所佔比例越大和越大所佔比例越小的兩個例子,

來源:互聯網
上載者:User

android:layout_weight越大所佔比例越大和越大所佔比例越小的兩個例子,

摘要: 我的技術部落格經常被流氓網站惡意爬取轉載。請移步原文:http://www.cnblogs.com/hamhog/p/3907146.html,享受整齊的排版、有效連結、正確的代碼縮排、更好的閱讀體驗。

關於android:layout_weight到底是什麼含義,在網上“越大所佔比例越大”和“越大所佔比例越小”兩種說法都有。實際上這兩種情況也都有。

例1:0dp

<LinearLayout android:orientation="vertical"         android:layout_width="match_parent"         android:layout_height="match_parent">    <TextView  android:layout_width="fill_parent" android:layout_height="0dp" android:background="#ff0000" android:layout_weight="1" android:text="1"/>    <TextView  android:layout_width="fill_parent" android:layout_height="0dp" android:background="#00ff00" android:layout_weight="2"  android:text="2"/></LinearLayout>

例2:

<LinearLayout android:orientation="vertical"         android:layout_width="match_parent"         android:layout_height="match_parent">    <TextView  android:layout_width="fill_parent" android:layout_height="match_parent" android:background="#ff0000" android:layout_weight="1" android:text="1"/>    <TextView  android:layout_width="fill_parent" android:layout_height="match_parent" android:background="#00ff00" android:layout_weight="2"  android:text="2"/></LinearLayout>

對兩種情況的解釋:

布局首先會給每個元素分配寬度,剩餘的空間再分配給每個元素。

在分配剩餘空間階段,比例按照layout_weight,也就是越大分得越大。

在情況一裡,固定寬度為0,只有第二階段;情況二裡,分配寬度能把所有空間分滿,剩餘的空間是負數,所以分配的越大,最終空間越小。

所以如果你要嚴格地讓它按照某種比例,可以試試定為0dp。此時各個元素的比例即為weight。


LinearLayout中的子項目屬性android:layout_weight有其他的特點哦

不知道你問的是什麼 我目前這個屬性的用法 比如一行有2個控制項,然後每個控制項
android:layout_weight=“1” 他們就平分這個地區
再比如 你有個TextView 下面一個ListView 為了讓listview下拉時不蓋掉上面的textview 只需給listview 設定android:layout_weight=“1” 就可以了
 
android編程android:layout_weight="1"報錯“Invalid layout param in a RelativeLayout: layout_weight

親,android:layout_weight只適用於線性布局LinearLayout,不適用於相對布局RelativeLayout啊
 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.