Android 中权重layout_weight
所谓的权重是按比例分配屏幕吗?
if(wrap_content){
那么权重越大,所占空间越大,小小不过wrap_content
}
if(match_parent){
那么权重越大,所占空间越小,大大不过match_parent
}
所以权重不是按比例分配屏幕
权重:将屏幕剩余空间按一定比例分配给空间
假设有两个水平排列Button按钮Btn1 和 Btn2 比例为1:1
屏幕宽度:ScreenWidth
If按钮为wrap_content类型
Then
屏幕剩余空间surplusWidth:screenWidth-2*wrap_conten
Btn1宽:wrap_conten+surplusWidth*1/2
Btn2宽:wrap_conten+surplusWidth*1/2
当比例为1:1000时
Btn1宽:wrap_conten+surplusWidth*1/1001
Btn2宽:wrap_conten+surplusWidth*1000/1001
If按钮为match_parent类型,比例为1:1时
Then
屏幕剩余空间surplusWidth:screenWidth-2*match_parent
即屏幕剩余空间就是就是一个负值
Btn1宽:match_parent+surplusWidth*1/2
Btn2宽:match_parent+surplusWidth*1/2
当比例为1:1000时
Btn1宽:match_parent+surplusWidth*1/1001
Btn2宽:match_parent+surplusWidth*1000/1001
因为surplusWidth为负值,所以比例越大,组件越小
如果想通过权重让组件完全按照比例显示该怎么办?
权重是可以让组件按照比例显示的,这种情况就是屏幕的空间等于剩余空间。即对应的宽或高设置为0。
以垂直线性布局为例:android:layout_height="0dp"
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<ImageView
android:id="@+id/splash_logo_iv"
android:layout_width="wrap_content"
android:layout_height="0dp"
android:layout_weight="15"
android:src="@drawable/ ic_launcher" />
<TextView
android:layout_width="wrap_content"
android:layout_height="0dp"
android:layout_gravity="center_horizontal"
android:layout_weight="1"
android:text="text"/>
</LinearLayout>