android屏幕适配中有一项比较常见的使用方式是使用android:layout_weight,按比例分配,大家都很常用,这里只mark一下计算的规则
权重控件的使用必须在LinearLayout中使用,先看一下不同属性下的不同效果
1.当权重属性为match_parent的时候
<LinearLayout android:layout_width="match_parent" android:layout_height="match_parent"> <TextView android:layout_width="match_parent" //注意这一块 android:layout_height="match_parent" android:layout_weight="1" android:background="@color/red" /> <TextView android:layout_width="match_parent" //<----- android:layout_height="match_parent" android:layout_weight="2" android:background="@color/green" /> </LinearLayout>效果如下:
2.当属性为wrap_content的时候
<LinearLayout android:layout_width="match_parent" android:layout_height="match_parent"> <TextView android:layout_width="wrap_content" android:layout_height="match_parent" android:layout_weight="1" android:background="@color/red" /> <TextView android:layout_width="wrap_content" android:layout_height="match_parent" android:layout_weight="2" android:background="@color/green" /> </LinearLayout>
3.当属性值为0的时候
<LinearLayout android:layout_width="match_parent" android:layout_height="match_parent"> <TextView android:layout_width="0dp" android:layout_height="match_parent" android:layout_weight="1" android:background="@color/red" /> <TextView android:layout_width="0dp" android:layout_height="match_parent" android:layout_weight="2" android:background="@color/green" /> </LinearLayout>
基本上每个android开发者都会用,这里记一下它的计算规则
显示大小 = 控件的大小+剩余的大小 * 权重的比值
如:2/3L = L + (L - 2L)*1/3
注:假设屏幕的宽度是L
纵向的weight的使用方式也是如此
打完收工