android:layout_weight 属性是在LinearLayout 为各个子视图分配权重,说的通俗点就是要分配多大的视图,比如如果两个视图在全部视图里面设置为1,1,那么就是平分父视图,但是在和match_parent使用的过程中,发现有的时候并不能评分,最后在一篇大神的博客上看到了答案。
android:layout_weight的真实含义是:一旦View设置了该属性(假设有效的情况下),那么该 View的宽度等于原有宽度(android:layout_width)加上剩余空间的占比!
举个例子,下面这个布局,在TableRow里面放两个
<TableRow android:layout_width="match_parent" android:layout_height="match_parent" android:layout_weight="1"> <LinearLayout android:orientation="horizontal" android:layout_width="match_parent" android:layout_height="match_parent" android:layout_weight="1" > <TextView android:text="2017年1月19日" android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/textView" android:layout_gravity="center"/> </LinearLayout> <LinearLayout android:orientation="horizontal" android:layout_width="match_parent" android:layout_height="match_parent" android:layout_weight="1" android:padding="40px"> <ImageView android:layout_width="wrap_content" android:layout_height="wrap_content" app:srcCompat="@drawable/iconfile1" android:id="@+id/imageView3" android:layout_weight="1" /> <ImageView android:layout_width="wrap_content" android:layout_height="wrap_content" app:srcCompat="@drawable/iconfile12" android:id="@+id/imageView2" android:layout_weight="1" android:layout_marginLeft="30px"/> <ImageView android:layout_width="wrap_content" android:layout_height="wrap_content" app:srcCompat="@drawable/iconfile13" android:id="@+id/imageView" android:layout_weight="1" android:layout_marginLeft="30px"/> </LinearLayout> </TableRow>
设屏幕宽度为L,这两个LinearLayout的宽度是match_parent,就是L,字啊
版权属于:东哥笔记 - DongGe.org
本文链接:https://dongge.org/blog/397.html
本文采用知识共享署名4.0 国际许可协议进行许可。转载或大段使用必须添加本文链接,否则您将构成侵权!