android之layout_weight使用
16lz
2021-01-23
<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】两个按钮的宽度各占屏幕的一半
- Android新增AppCompatTextView自适应字体大小和文本宽度
- C++中动态内存分配与命名空间介绍
- c语言怎么实现动态内存分配
- c语言数组在内存中是怎么分配的?
- 简述分配器的作用是什么?
- Kafka 原理以及分区分配策略剖析
- 操作系统分配存储时网络无法正常连接
- Python 内存分配时的小秘密