android:layout_weight的真实含义

转自:http://blog.youkuaiyun.com/yanzi1225627/article/details/24667299

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

[html]  view plain copy print ? 在CODE上查看代码片 派生到我的代码片
  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 ? 在CODE上查看代码片 派生到我的代码片
  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就可以理解为占比了!

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" android:background="#ECECEC" android:padding="10dp"> <LinearLayout android:layout_width="match_parent" android:layout_height="0dp" android:layout_weight="0.4" android:orientation="horizontal"> <ImageView android:padding="2dp" android:layout_width="30dp" android:layout_height="30dp" android:src="@drawable/img_jisuanqi"/> <TextView android:layout_width="0dp" android:layout_height="30dp" android:layout_weight="1" android:text="计算器" android:textColor="#000000" android:textSize="15sp" android:layout_marginLeft="8dp" android:gravity="center_vertical"/> <ImageView android:layout_width="60dp" android:layout_height="30dp" android:src="@drawable/img_jianhao" android:padding="6dp"/> <ImageView android:layout_width="60dp" android:layout_height="30dp" android:padding="6dp" android:src="@drawable/img_square"/> <ImageView android:layout_width="60dp" android:layout_height="30dp" android:padding="6dp" android:src="@drawable/img_cha"/> </LinearLayout> <LinearLayout android:layout_width="match_parent" android:layout_height="0dp" android:layout_weight="0.6" android:orientation="horizontal" android:layout_marginTop="10dp" android:gravity="center"> <ImageView android:padding="6dp" android:layout_width="30dp" android:layout_height="30dp" android:src="@drawable/img_gang"/> <TextView android:layout_width="wrap_content" android:layout_height="match_parent" android:text="标准" android:textColor="#000000" android:textSize="30sp" android:textStyle="bold" android:layout_marginLeft="8dp" android:gravity="center_vertical"/> <ImageView android:layout_width="0dp" android:layout_height="30dp" android:layout_weight="1" android:scaleType="fitStart" android:src="@drawable/img_top"/> <ImageView android:layout_width="30dp" android:layout_height="30dp" android:padding="6dp" android:src="@drawable/img_lishi"/> </LinearLayout>解析
10-21
那你帮我在这个基础上进行最小量的修改<?xml version="1.0" encoding="utf-8"?> <androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:background="@color/surface_background"> <!-- 🔹 上方搜索栏容器 --> <LinearLayout android:layout_marginTop="?attr/actionBarSize" android:id="@+id/search_container" android:layout_width="0dp" android:layout_height="wrap_content" android:orientation="horizontal" android:padding="8dp" app:layout_constraintTop_toTopOf="parent" app:layout_constraintStart_toStartOf="parent" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintWidth_percent="0.95" /> <!-- EditText 输入框 --> <EditText android:id="@+id/search_input" android:layout_width="0dp" android:layout_height="48dp" android:layout_weight="1" android:hint="搜索公交站或线路" android:padding="12dp" android:background="@drawable/rounded_edittext" android:layout_marginEnd="8dp" android:inputType="text" app:layout_constraintStart_toStartOf="parent" app:layout_constraintEnd_toStartOf="@id/search_btn" app:layout_constraintTop_toTopOf="@id/search_container" app:layout_constraintBottom_toBottomOf="@id/search_container" /> <!-- 搜索按钮 --> <Button android:id="@+id/search_btn" android:layout_width="wrap_content" android:layout_height="48dp" android:text="搜索" app:layout_constraintStart_toEndOf="@id/search_input" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintTop_toTopOf="@id/search_container" app:layout_constraintBottom_toBottomOf="@id/search_container" /> <!-- 🗺️ 地图视图:占页面中间 40% 高度 --> <com.amap.api.maps.MapView android:id="@+id/map_view" android:layout_width="0dp" android:layout_height="0dp" app:layout_constraintTop_toBottomOf="@id/search_container" app:layout_constraintStart_toStartOf="parent" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintHeight_percent="0.4" app:layout_constraintWidth="wrap_content" android:layout_marginTop="10dp" android:layout_marginHorizontal="16dp" /> <!-- 🔽 RecyclerView:结果列表,占据剩余空间 --> <androidx.recyclerview.widget.RecyclerView android:id="@+id/result_list" android:layout_width="0dp" android:layout_height="0dp" app:layout_constraintTop_toBottomOf="@id/map_view" app:layout_constraintStart_toStartOf="parent" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintBottom_toTopOf="@id/btn_go_to" app:layout_constraintVertical_bias="0" android:layout_marginTop="8dp" android:layout_marginHorizontal="16dp" /> <!-- “到这去”按钮:固定在底部上方 --> <Button android:id="@+id/btn_go_to" android:layout_width="0dp" android:layout_height="wrap_content" android:text="到这去" android:layout_margin="16dp" app:layout_constraintStart_toStartOf="parent" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintBottom_toBottomOf="parent" /> </androidx.constraintlayout.widget.ConstraintLayout> 用guideline和space的结合给我提供一份完整代码
最新发布
11-07
<?xml version="1.0" encoding="utf-8"?> <androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:id="@+id/main" android:layout_width="match_parent" android:layout_height="match_parent" tools:context=".MainActivity"> <!-- activity_profile_skeleton.xml --> <!-- activity_profile.xml --> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" android:padding="16dp"> <ImageView android:id="@+id/ivAvatar" android:layout_width="80dp" android:layout_height="80dp" android:src="@drawable/timer" /> <TextView android:id="@+id/tvName" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="用户名" android:textSize="20sp" android:textStyle="bold" android:layout_marginTop="16dp"/> <TextView android:id="@+id/tvBio" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="用户简介内容..." android:layout_marginTop="8dp"/> <View android:layout_width="match_parent" android:layout_height="1dp" android:background="#CCCCCC" android:layout_marginTop="16dp"/> <TextView android:id="@+id/tvPostsTitle" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="动态" android:textSize="18sp" android:layout_marginTop="16dp"/> <ListView android:id="@+id/lvPosts" android:layout_width="match_parent" android:layout_height="0dp" android:layout_weight="1" android:layout_marginTop="8dp"/> </LinearLayout> </androidx.constraintlayout.widget.ConstraintLayout>这是原布局,对应xml是啥
08-27
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值