摘要
平時沒那麼注意linearlayout布局時權重的問題,設定了權重屬性後,通常建議将width或height的屬性值設定為0dp,有時候設定權重後,還是習慣将width或height的屬性設定為wrap_content,這會有什麼影響嗎?做完了“掌上平桂”項目後,發現新聞欄目的多圖展示,總是出現三張圖無法平均配置設定空間的問題,其中一個原因,每一張圖檔的尺寸不同,最初的猜想可能網絡加載資料延時的問題或是viewholder類的問題。最後發現原因是權重設定的問題。
二.多張圖布局設計
使用relativelayout布局,嵌套垂直的linearlayout,linearlayout嵌套textview和另一個水準的linearlayout,水準的linearlayout放置三張圖檔,最初水準的linearlayout代碼如下:
<linearlayout
android:id="@+id/external_news_horizontal_ll"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal" >
<imageview
android:id="@+id/news_list_item_img_one_iv"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_<a title="【檢視含有[weight]标簽的文章】" href="http://teachcourse.cn/tag/weight" target="_blank">weight</a>="1"
android:contentdescription="@string/display_news"
android:layout_marginright="@dimen/hot_news_img_list_left"
android:src="@drawable/default_bg"
android:scaletype="centercrop"/>
<imageview
android:id="@+id/news_list_item_img_two_iv"
android:layout_weight="1"
android:scaletype="centercrop"
android:src="@drawable/default_bg"/>
android:id="@+id/news_list_item_img_three_iv"
</linearlayout>
網絡加載多圖請求後,在baseadapter擴充卡中填充擷取的圖檔 内容後,出現多張圖檔配置設定不均勻的情況,但部分圖檔配置設定是均勻的,這就讓teachcourse感覺更奇怪,布局中設定的權重都一樣的,适配時為什麼有的三張圖占的空間不一樣。
通常,遇到一個問題,擱在心裡teachcourse覺得挺難受,根據程式設計的感覺,可以肯定某個地方的代碼是有問題的,否則不會出現這種情況。昨晚,第一感覺應該是baseadapter使用viewholder設定标簽的問題,本來是直接寫:
mviewholder.imageview.setimagebitmap();
改成了
imageview imageview=mviewholder.imageview;
imageview.setimagebitmap();
認為擷取是對象指派的問題導緻的,第二種可能網絡加載圖檔資料的問題,測試後發現還是一樣,後來檢視了一下布局檔案,如上述布局代碼
最大的可能,出現在了linearlayout布局中imageview标簽設定width和height的問題,上述代碼中每個imageview設定的width和height都為wrap_content,同時都設定權重1,似乎不起作用。于是嘗試将權重去掉,發現三張圖的,最後隻顯示兩張,基本空間都是配置設定不均勻,看來問題大概明确,權重設定不合理,将width設定的wrap_content改為0dp,修改後的代碼:
android:id="@+id/news_list_item_img_one_iv"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:contentdescription="@string/display_news"
android:layout_marginright="@dimen/hot_news_img_list_left"
android:src="@drawable/default_bg"
android:scaletype="centercrop"/>
android:id="@+id/news_list_item_img_two_iv"
android:scaletype="centercrop"
android:src="@drawable/default_bg"/>
android:id="@+id/news_list_item_img_three_iv"
ps:水準的linearlayout布局,設定權重,width應該設定0dp;垂直的linearlayout布局,設定權重,height應該設定0dp,否則可能出現width或height配置設定不均勻的情況,最終原因權重設定不生效。
布局調整前後,加載網絡圖檔展示,明顯差別
本文作者:佚名
來源:51cto