关于LinearLayout设置权重后width或height不设置0dp的影响说明

2016-07-29 11:20 评论 0 条

一.摘要

平时没那么注意LinearLayout布局时权重的问题,设置了权重属性后,通常建议将width或height的属性值设置为0dp,有时候设置权重后,还是习惯将width或height的属性设置为wrap_content,这会有什么影响吗?做完了“掌上平桂”项目后,发现新闻栏目的多图展示,总是出现三张图无法平均分配空间的问题,其中一个原因,每一张图片的尺寸不同,最初的猜想可能网络加载数据延时的问题或是ViewHolder类的问题。最后发现原因是权重设置的问题。

二.多张图布局设计

使用RelativeLayout布局,嵌套垂直的LinearLayout,LinearLayout嵌套TextView和另一个水平的LinearLayout,水平的LinearLayout放置三张图片,最初水平的LinearLayout代码如下:

  1. <LinearLayout  
  2.             android:id="@+id/external_news_horizontal_ll"  
  3.             android:layout_width="match_parent"  
  4.             android:layout_height="match_parent"  
  5.             android:orientation="horizontal" >  
  6.   
  7.             <ImageView  
  8.                 android:id="@+id/news_list_item_img_one_iv"  
  9.                 android:layout_width="wrap_content"  
  10.                 android:layout_height="wrap_content"  
  11.                 android:layout_weight="1"  
  12.                 android:contentDescription="@string/display_news"  
  13.                 android:layout_marginRight="@dimen/hot_news_img_list_left"  
  14.                 android:src="@drawable/default_bg"  
  15.                 android:scaleType="centerCrop"/>  
  16.   
  17.             <ImageView  
  18.                 android:id="@+id/news_list_item_img_two_iv"  
  19.                 android:layout_width="wrap_content"  
  20.                 android:layout_height="wrap_content"  
  21.                 android:layout_weight="1"  
  22.                 android:contentDescription="@string/display_news"  
  23.                 android:layout_marginRight="@dimen/hot_news_img_list_left"  
  24.                 android:scaleType="centerCrop"  
  25.                 android:src="@drawable/default_bg"/>  
  26.   
  27.             <ImageView  
  28.                 android:id="@+id/news_list_item_img_three_iv"  
  29.                 android:layout_width="wrap_content"  
  30.                 android:layout_height="wrap_content"  
  31.                 android:layout_weight="1"  
  32.                 android:contentDescription="@string/display_news"  
  33.                 android:layout_marginRight="@dimen/hot_news_img_list_left"  
  34.                 android:src="@drawable/default_bg"  
  35.                 android:scaleType="centerCrop"/>  
  36.         </LinearLayout>  

网络加载多图请求后,在BaseAdapter适配器中填充获取的图片 内容后,出现多张图片分配不均匀的情况,但部分图片分配是均匀的,这就让TeachCourse感觉更奇怪,布局中设置的权重都一样的,适配时为什么有的三张图占的空间不一样。

001-android-layout-weight

通常,遇到一个问题,搁在心里TeachCourse觉得挺难受,根据编程的感觉,可以肯定某个地方的代码是有问题的,否则不会出现这种情况。昨晚,第一感觉应该是BaseAdapter使用ViewHolder设置标签的问题,本来是直接写:

  1. mViewHolder.imageView.setImageBitmap();  

改成了

  1. ImageView imageView=mViewHolder.imageView;  
  2. imageView.setImageBitmap();  

认为获取是对象赋值的问题导致的,第二种可能网络加载图片数据的问题,测试后发现还是一样,后来查看了一下布局文件,如上述布局代码

最大的可能,出现在了LinearLayout布局中ImageView标签设置width和height的问题,上述代码中每个ImageView设置的width和height都为wrap_content,同时都设置权重1,似乎不起作用。于是尝试将权重去掉,发现三张图的,最后只显示两张,基本空间都是分配不均匀,看来问题大概明确,权重设置不合理,将width设置的wrap_content改为0dp,修改后的代码:

  1. <LinearLayout  
  2.             android:id="@+id/external_news_horizontal_ll"  
  3.             android:layout_width="match_parent"  
  4.             android:layout_height="match_parent"  
  5.             android:orientation="horizontal" >  
  6.   
  7.             <ImageView  
  8.                 android:id="@+id/news_list_item_img_one_iv"  
  9.                 android:layout_width="0dp"  
  10.                 android:layout_height="wrap_content"  
  11.                 android:layout_weight="1"  
  12.                 android:contentDescription="@string/display_news"  
  13.                 android:layout_marginRight="@dimen/hot_news_img_list_left"  
  14.                 android:src="@drawable/default_bg"  
  15.                 android:scaleType="centerCrop"/>  
  16.   
  17.             <ImageView  
  18.                 android:id="@+id/news_list_item_img_two_iv"  
  19.                 android:layout_width="0dp"  
  20.                 android:layout_height="wrap_content"  
  21.                 android:layout_weight="1"  
  22.                 android:contentDescription="@string/display_news"  
  23.                 android:layout_marginRight="@dimen/hot_news_img_list_left"  
  24.                 android:scaleType="centerCrop"  
  25.                 android:src="@drawable/default_bg"/>  
  26.   
  27.             <ImageView  
  28.                 android:id="@+id/news_list_item_img_three_iv"  
  29.                 android:layout_width="0dp"  
  30.                 android:layout_height="wrap_content"  
  31.                 android:layout_weight="1"  
  32.                 android:contentDescription="@string/display_news"  
  33.                 android:layout_marginRight="@dimen/hot_news_img_list_left"  
  34.                 android:src="@drawable/default_bg"  
  35.                 android:scaleType="centerCrop"/>  
  36.         </LinearLayout>  

PS:水平的LinearLayout布局,设置权重,width应该设置0dp;垂直的LinearLayout布局,设置权重,height应该设置0dp,否则可能出现width或height分配不均匀的情况,最终原因权重设置不生效。
002-android-layout-weight

布局调整前后,加载网络图片展示,明显区别

003-android-layout-wrap-content

当前文章价值0.13元,扫一扫支付后添加微信提供帮助!(如不能解决您的问题,可以申请退款)

你可能感兴趣的文章

来源:每日教程每日一例,深入学习实用技术教程,关注公众号TeachCourse
转载请注明出处: https://www.teachcourse.cn/1974.html ,谢谢支持!

资源分享

分类:Android 标签:,
”Found 2 version of android-support-v4.jar in the dependency list“解决思路 ”Found 2 version of android-
浅谈JDK和JRE 浅谈JDK和JRE
Android开发之深入理解工厂(Factory)模式 Android开发之深入理解工厂(F
一张图看懂日志收集模块 一张图看懂日志收集模块

发表评论

呲牙 憨笑 坏笑 偷笑 色 微笑 抓狂 睡觉 酷 流汗 鼓掌 大哭 可怜 疑问 晕 惊讶 得意 尴尬 发怒 奋斗 衰 骷髅 啤酒 吃饭 礼物 强 弱 握手 OK NO 勾引 拳头 差劲 爱你

表情