android中的layout_weight的含义

本文深入探讨了Android中LinearLayout布局下layout_weight属性的工作原理,并通过实例对比了不同layout_width设置下权重分配的区别。

首先声明只有在Linearlayout中,该属性才有效。之所以Android:layout_weight会引起争议,是因为在设置该属性的同时,设置android:layout_width为wrap_content和match_parent会造成两种截然相反的效果。如下所示:

[html]  view plain  copy
 print ?
  1. <LinearLayout  
  2.        android:layout_width="match_parent"  
  3.        android:layout_height="wrap_content"  
  4.        android:orientation="horizontal" >  
  5.   
  6.        <TextView  
  7.            android:layout_width="match_parent"  
  8.            android:layout_height="wrap_content"  
  9.            android:layout_weight="1"  
  10.            android:background="@android:color/black"  
  11.            android:text="111"  
  12.            android:textSize="20sp" />  
  13.   
  14.        <TextView  
  15.            android:layout_width="match_parent"  
  16.            android:layout_height="wrap_content"  
  17.            android:layout_weight="2"  
  18.            android:background="@android:color/holo_green_light"  
  19.            android:text="222"  
  20.            android:textSize="20sp" />  

上面的布局将两个TextView的宽度均设为match_parent,一个权重为1,一个权重为2.得到效果如下:


可以看到权重为1的反而占了三分之二!

再看如下布局:

[html]  view plain  copy
 print ?
  1. <LinearLayout  
  2.     android:layout_width="match_parent"  
  3.     android:layout_height="wrap_content"  
  4.     android:orientation="horizontal" >  
  5.   
  6.     <TextView  
  7.         android:layout_width="wrap_content"  
  8.         android:layout_height="wrap_content"  
  9.         android:layout_weight="1"  
  10.         android:background="@android:color/black"  
  11.         android:text="111"  
  12.         android:textSize="20sp" />  
  13.   
  14.     <TextView  
  15.         android:layout_width="wrap_content"  
  16.         android:layout_height="wrap_content"  
  17.         android:layout_weight="2"  
  18.         android:background="@android:color/holo_green_light"  
  19.         android:text="222"  
  20.         android:textSize="20sp" />  
  21. </LinearLayout>  

即宽度为wrap_content,得到视图如下:


左边 TextView占比三分之一,又正常了。

android:layout_weight的真实含义是:一旦View设置了该属性(假设有效的情况下),那么该 View的宽度等于原有宽度(android:layout_width)加上剩余空间的占比!

设屏幕宽度为L,在两个view的宽度都为match_parent的情况下,原有宽度为L,两个的View的宽度都为L,那么剩余宽度为L-(L+L) = -L, 左边的View占比三分之一,所以总宽度是L+(-L)*1/3 = (2/3)L.事实上默认的View的weight这个值为0,一旦设置了这个值,那么所在view在绘制的时候执行onMeasure两次的原因就在这。

Google官方推荐,当使用weight属性时,将width设为0dip即可,效果跟设成wrap_content是一样的。这样weight就可以理解为占比了!

<com.mediatek.keyguard.Telephony.KeyguardSimPinPukMeView xmlns:android="http://schemas.android.com/apk/res/android" xmlns:androidprv="http://schemas.android.com/apk/res-auto" android:id="@+id/keyguard_sim_pin_puk_me_view" android:orientation="vertical" android:layout_width="match_parent" android:layout_height="match_parent" androidprv:layout_maxWidth="@dimen/keyguard_security_width" androidprv:layout_maxHeight="@dimen/mtk_keyguard_security_max_height" android:gravity="center_horizontal"> <LinearLayout android:id="@+id/sim_info_message" android:layout_width="wrap_content" android:layout_height="wrap_content" android:orientation="horizontal" android:layout_gravity="center_horizontal" android:gravity="center_vertical"> <TextView android:id="@+id/slot_num_text" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center_vertical" android:ellipsize="marquee" android:singleLine="true" android:textDirection = "locale" android:textAppearance="?android:attr/textAppearanceLarge" android:textColor="?attr/wallpaperTextColor" android:visibility="gone" /> <ImageView android:id="@+id/keyguard_sim" android:layout_width="match_parent" android:layout_height="wrap_content" android:src="@drawable/ic_lockscreen_sim"/> </LinearLayout> <LinearLayout android:id="@+id/keyguard_bouncer_frame" android:layout_width="match_parent" android:layout_height="0dp" android:orientation="vertical" android:layout_weight="1" android:layoutDirection="ltr" > <RelativeLayout android:id="@+id/row0" android:layout_width="match_parent" android:layout_height="0dp" android:layout_weight="1" android:paddingBottom="16dp" > <com.android.keyguard.PasswordTextView android:id="@+id/simPinPukMeEntry" android:layout_width="@dimen/keyguard_security_width" android:layout_height="match_parent" android:gravity="center" android:layout_centerHorizontal="true" android:layout_marginRight="72dp" androidprv:scaledTextSize="28" /> <com.android.keyguard.NumPadButton android:id="@+id/delete_button" android:layout_width="wrap_content" android:layout_height="match_parent" android:gravity="center_vertical" android:clickable="true" android:paddingTop="8dip" android:paddingBottom="8dip" android:paddingRight="8dp" android:paddingLeft="24dp" android:contentDescription="@string/keyboardview_keycode_delete" android:layout_alignEnd="@+id/simPinPukMeEntry" android:layout_alignParentRight="true" style="@style/NumPadKey.Delete" /> <View android:id="@+id/divider" android:layout_width="match_parent" android:layout_height="1dp" android:layout_alignParentBottom="true" android:background="#28FFFFFF" /> </RelativeLayout> <LinearLayout android:layout_width="match_parent" android:layout_height="0dp" android:layout_weight="1" android:orientation="horizontal" > <com.android.keyguard.NumPadKey android:id="@+id/key1" android:layout_width="0px" android:layout_height="match_parent" android:layout_weight="1" android:layout_marginEnd="@dimen/mtk_num_pad_key_margin_end" androidprv:textView="@+id/simPinPukMeEntry" androidprv:digit="1" /> <com.android.keyguard.NumPadKey android:id="@+id/key2" android:layout_width="0px" android:layout_height="match_parent" android:layout_weight="1" android:layout_marginEnd="@dimen/mtk_num_pad_key_margin_end" androidprv:textView="@+id/simPinPukMeEntry" androidprv:digit="2" /> <com.android.keyguard.NumPadKey android:id="@+id/key3" android:layout_width="0px" android:layout_height="match_parent" android:layout_weight="1" androidprv:textView="@+id/simPinPukMeEntry" androidprv:digit="3" /> </LinearLayout> <LinearLayout android:layout_width="match_parent" android:layout_height="0dp" android:layout_weight="1" android:orientation="horizontal" > <com.android.keyguard.NumPadKey android:id="@+id/key4" android:layout_width="0px" android:layout_height="match_parent" android:layout_weight="1" android:layout_marginEnd="@dimen/mtk_num_pad_key_margin_end" androidprv:textView="@+id/simPinPukMeEntry" androidprv:digit="4" /> <com.android.keyguard.NumPadKey android:id="@+id/key5" android:layout_width="0px" android:layout_height="match_parent" android:layout_weight="1" android:layout_marginEnd="@dimen/mtk_num_pad_key_margin_end" androidprv:textView="@+id/simPinPukMeEntry" androidprv:digit="5" /> <com.android.keyguard.NumPadKey android:id="@+id/key6" android:layout_width="0px" android:layout_height="match_parent" android:layout_weight="1" androidprv:textView="@+id/simPinPukMeEntry" androidprv:digit="6" /> </LinearLayout> <LinearLayout android:layout_width="match_parent" android:layout_height="0dp" android:orientation="horizontal" android:layout_weight="1" > <com.android.keyguard.NumPadKey android:id="@+id/key7" android:layout_width="0px" android:layout_height="match_parent" android:layout_weight="1" android:layout_marginEnd="@dimen/mtk_num_pad_key_margin_end" androidprv:textView="@+id/simPinPukMeEntry" androidprv:digit="7" /> <com.android.keyguard.NumPadKey android:id="@+id/key8" android:layout_width="0px" android:layout_height="match_parent" android:layout_weight="1" android:layout_marginEnd="@dimen/mtk_num_pad_key_margin_end" androidprv:textView="@+id/simPinPukMeEntry" androidprv:digit="8" /> <com.android.keyguard.NumPadKey android:id="@+id/key9" android:layout_width="0px" android:layout_height="match_parent" android:layout_weight="1" androidprv:textView="@+id/simPinPukMeEntry" androidprv:digit="9" /> </LinearLayout> <LinearLayout android:layout_width="match_parent" android:layout_height="0dp" android:layout_weight="1" android:orientation="horizontal" > <Button android:id="@+id/key_dismiss" style="@style/DismissPadKey" android:layout_width="0px" android:layout_height="match_parent" android:layout_weight="1" android:layout_marginEnd="@dimen/mtk_num_pad_key_margin_end" android:visibility="invisible" android:text="DISMISS" /> <com.android.keyguard.NumPadKey android:id="@+id/key0" android:layout_width="0px" android:layout_height="match_parent" android:layout_weight="1" android:layout_marginEnd="@dimen/mtk_num_pad_key_margin_end" androidprv:textView="@+id/simPinPukMeEntry" androidprv:digit="0" /> <com.android.keyguard.NumPadButton android:id="@+id/key_enter" android:layout_width="0px" android:layout_height="match_parent" android:layout_weight="1" style="@style/NumPadKey.Enter" android:contentDescription="@string/keyboardview_keycode_enter" /> </LinearLayout> </LinearLayout> <include layout="@layout/keyguard_eca" android:id="@+id/keyguard_selector_fade_container" android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="vertical" android:layout_gravity="bottom|center_horizontal" android:gravity="center_horizontal"/> </com.mediatek.keyguard.Telephony.KeyguardSimPinPukMeView> 该界面数字键显示不全,分析问题并修改布局
最新发布
11-28
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值