android:layout_weight 此属性严格的说法应该是对当前剩余空间按权重平分


我们平时常会用到android:layout_weight 这个属性来对UI空间进行百分比划分,一个是使界面看起来比较整齐,还有就是比较容易调整。

根据android:layout_weight 设置的实例:

布局文件代码如下:

<LinearLayout    android:layout_width="match_parent"    android:layout_height="match_parent"    android:gravity="center" >    <TextView        android:id="@+id/main_i_edittext"        android:layout_width="0dp"        android:layout_height="wrap_content"        android:layout_marginLeft="5dp"        android:layout_weight="8"        android:background="@drawable/rounded_edittext"        android:gravity="center"        android:singleLine="true"        android:clickable="true"        android:focusable="true"        android:onClick="main_click_i"        android:text="@string/default_value_0"        android:textColor="#000000"        android:textSize="20sp" />    <TextView        android:id="@+id/main_i_textview2"        android:layout_width="match_parent"        android:layout_height="wrap_content"        android:layout_marginLeft="5dp"        android:layout_weight="2"        android:singleLine="true"        android:gravity="bottom"        android:text="A"        android:textColor="#00f"        android:textSize="17sp" />LinearLayout>

这里我采用了最常用的0dp作为Texview的宽度,下面我们来看一下三种不同的宽度参数对权重参数的影响
借用一张以前看到的图,^_^

每个LinearLayout内部三个TextView的layout_weight分别为1,2,3。由于其wrap_content的不同,使其layout_weight的分配受到了影响。这里就来layout_weight按屏幕剩余空间按权重分配的情况。

第一种情况(android:layout_height=”wrap_content”)
系统给3个TextView分配他们的宽度值wrap_content(宽度足以包含他们的内容即可),然后会把剩下来的屏幕空间按照1:2:3的比列分配给3个textview,
上面的UI 比重为 :6*1/6 ,6*2/6,6*3/6 即1:2:3 ,如UI 第一行呈现的那样。

第二种情况(android:layout_height=” match_parent “)
系统先给3个textview分配他们所要的宽度match_parent,也就是说每一都是填满他的父控件。

假设屏幕宽度为1。那么三个控件占据后的剩余空间就是1-1*3=-2;然后将剩余的-2宽度屏幕控件按照1:2:3的比例分给三个控件。
A:-2*(1/6)=-1/3 ; 然后原来分配给控件的宽度为1;1+(-1/3)=2/3;故实际宽度为2/3
B:-2*(2/6)=-2/3 ;同上,1+(-2/3)=1/3;故实际宽度为1/3
C:-2*(3/6)=-1;同上,1+(-1)=0;故实际宽度为0,这也就是为什么C显示不出来的原因了

第三种情况(android:layout_height=” 0dp”)
这种情况,不管TextView的内容宽度,直接将屏幕剩余空间按照1:2:3的比例分配给三个TextView。UI 比重虽然也是1:2:3,但是与第一种情况还是有一点区别。


这里我贴的是第三种情况的代码:

<LinearLayout    android:layout_width="match_parent"    android:layout_height="wrap_content"    android:layout_marginTop="20dp"    android:orientation="horizontal">    <TextView        android:layout_width="0dp"        android:layout_height="wrap_content"        android:layout_weight="1"        android:background="#77bc1f"        android:text="AAA"        android:textColor="#fff"        android:textSize="23sp" />    <TextView        android:layout_width="0dp"        android:layout_height="wrap_content"        android:layout_weight="2"        android:background="#06d992"        android:text="BBB"        android:textColor="#fff"        android:textSize="23sp" />    <TextView        android:layout_width="0dp"        android:layout_height="wrap_content"        android:layout_weight="3"        android:background="#EEA32E"        android:text="CCC"        android:textColor="#fff"        android:textSize="23sp" />LinearLayout>

总结:

在使用android:layout_weight进行空间分配的时候我们要注意是对当前剩余空间按权重平分,觉得麻烦的话就设置组件宽度为0dp是最省心的了!

更多相关文章

  1. 快速开发框架Afinal的使用(数据库操作,HTTP请求,网络图片加载,控件绑
  2. Android自定义控件一简介
  3. Android界面布局的几种常用方式
  4. android 获取屏幕高度,宽度,状态栏高度
  5. Android事件分发机制
  6. 13-5-16 Android自定义空间实现wifi信号强度
  7. Android控件之TextView全解析
  8. Android(安卓)UI基本测验:线性布局
  9. Android(安卓)实用工具Hierarchy Viewer实战

随机推荐

  1. Android(安卓)NDK学习(6)在Android项目中调
  2. android 实现点击输入框弹出日期选择对话
  3. [房贷计算器]-升级心得
  4. Android(安卓)RxJava+Retrofit2+RxBindin
  5. Android(安卓)N SettingsProvider的数据
  6. Android(安卓)TextView 去除内边距
  7. Android系统启动流程源码分析
  8. Android(安卓)的Input Event 子系统的来
  9. Android中Activity的android:windowSoftI
  10. Android中杀死进程的方法