我是靠谱客的博主 悦耳路人,这篇文章主要介绍android layout_weight 属性使用方法,现在分享给大家,希望可以做个参考。

layout_weight 是android 线性布局LinearLayout里 控件一个很好用的属性,

layout_weight : 即为当前线性布局 的属性指定方向(水平、竖直)上 剩余空间 的比重。

注意事项:
1. 布局为 线性布局LinearLayout时 控件的属性layout_weight才有效
2. 只有当前控件宽度属性为0 layout_weight 才有效 : 即android:layout_width=”0dp” layout_weight 才有效
3. 比重原则:线性布局中, 当前控件 占 所有控件(layout_weight有效)的比例

请看eg:

复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
<TextView android:id="@+id/textview" android:layout_width="200dp" android:layout_weight="2" android:layout_height="wrap_content"/> <Button android:id="@+id/button1" android:layout_height="wrap_content" android:layout_width="0dp" android:layout_weight="2" android:text="button1"/> <Button android:id="@+id/button2" android:layout_height="wrap_content" android:layout_width="0dp" android:layout_weight="5" android:text="button2"/>

案例说明
textview
由于该控件的 layout_width 指定为 200dp 所以 layout_weight属性是无效的 该控件仍然按照layout_width布局

button1 和button2
由于这两个按钮的属性layout_width 都指定为0 所以layout_weight有效
button1 和button2 占据了线性布局 除去textview 剩余的部分
button1 所占宽度比例 2/(2+5)
button2 所占宽度比例 5/(2+5)

最后

以上就是悦耳路人最近收集整理的关于android layout_weight 属性使用方法的全部内容,更多相关android内容请搜索靠谱客的其他文章。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(49)

评论列表共有 0 条评论

立即
投稿
返回
顶部