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

简介:

摘要

平时没那么注意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_<a title="【查看含有[weight]标签的文章】" href="http://teachcourse.cn/tag/weight" target="_blank">weight</a>="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感觉更奇怪,布局中设置的权重都一样的,适配时为什么有的三张图占的空间不一样。

通常,遇到一个问题,搁在心里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分配不均匀的情况,最终原因权重设置不生效。

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





本文作者:佚名
来源:51CTO
目录
相关文章
|
8月前
|
编解码 Android开发
Android 常用布局单位区别(dp、sp、px、pt、in、mm)
Android 常用布局单位区别(dp、sp、px、pt、in、mm)
294 0
|
XML Android开发 数据格式
View默认的LayoutParams是何时生成的,默认值是什么。layout_width和layout_height属性在哪里生效
View默认的LayoutParams是何时生成的,默认值是什么。layout_width和layout_height属性在哪里生效
|
编解码 Android开发
关于Android获取屏幕宽高、dp、sp、px之间的转化
开发过程中,动态创建布局,或者自定义view,少不了需要获取屏幕宽高,这里的宽高指手机屏幕的分辨率,单位是px,而我们在布局文件中用到的空间宽高单位是dp,字体用的是sp。 这几个计量单位之间,是有关联的,比如dp与px,是通过density来相互转化的,px跟sp则通过scaledDensity来相互转化,类似于小学的除数、被除数、商,三者之间的关系。
371 0
|
Android开发
Android 动态修改layout_weight
Android 动态修改layout_weight
631 0
Android 动态修改layout_weight
|
Android开发 开发者
Android官方dip值到pix值转换:dip2pix,dip2px,dp2px实现
Android官方的dip to pix,dip2pix,dp2px实现 网上流传的一个常用的把dip值转换为pix像素值的方法通常是这样的: public static int dip2px(Context co...
2889 0
|
Java Android开发
【转】Android 为什么 dp2px 或 px2dp 公式需要加 0.5f
转自:http://blog.csdn.net/changcsw/article/details/52440543 网上 dp2px 和 px2dp 公式:   [java] view plain copy   public static int px2dip(Cont...
2196 0
|
Android开发 编解码