android之layout_weight使用
16lz
2021-12-04
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="horizontal" > <TextView android:layout_width="match_parent" android:layout_height="40dp" android:layout_weight="1"/> <TextView android:layout_width="match_parent" android:layout_height="40dp" android:layout_weight="2"/> <TextView android:layout_width="match_parent" android:layout_height="40dp" android:layout_weight="2"/> </LinearLayout>
这里为每个TextView设置的宽度为”match_parent",即为充满整个LinearLayout,假如屏幕依然为320dip, 首先LinearLayout为3个TextView分配的宽度为320dip,屏幕剩余宽度为 320 - 3* 320 = -640dip,然后3个TextView按照权重分配剩余空间,第一个TextView分配宽度为 320 + (-640) * (1/5) = 192dip,后面两个TextView就为320 + (-640) * (2/5) = 64dip,则三个TextView权重比例实际上为3:1:1,而不是1:2:2。
谷歌官方推荐如果设置layout_weight属性的时候,最好将layout_width设置为“0dp",这样才能按照1:2:2的比例为三个TextView分配空间。
stackoverflow.com上对剩余空间计算的解释。
http://stackoverflow.com/questions/7452741/what-is-androidweightsum-in-android-and-how-does-it-work
更多相关文章
- android LinearLayout 相关知识
- Android帧布局-实现渐变效果
- TextView的android:maxHeight,android:minHeight的正确设置
- EditText的一些使用注意点
- [Android] 零碎知识汇总 - Shape相关
- GridView控件的简单使用
- Android(安卓)Studio 布局属性笔记
- Android中android:layout_width与 android:width有什么区别?
- 【android】两个按钮的宽度各占屏幕的一半