我是靠谱客的博主 呆萌芒果,这篇文章主要介绍LinearLayout中的layout_weight属性详解,现在分享给大家,希望可以做个参考。

线性布局中的layout_weight顾名思义即为定义各个组件在整个界面中所占的权重。常用两种实现方法

    1:最为简单的实现方法,当orientation为水平时垂直layout_height=''0dp",同理当orientation为垂直时,将layout_width设定为0dp,此时在一个直接父布局中layout_width中的数值越大这个组件占的界面就越大。示例代码如下:

 

复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:id="@+id/LinearLayout1" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="horizontal" > <LinearLayout android:layout_width="0dp" android:layout_height="fill_parent" android:background="#ADFF2F" android:layout_weight="1"/> <LinearLayout android:layout_width="0dp" android:layout_height="fill_parent" android:background="#DA70D6" android:layout_weight="2"/> </LinearLayout>

运行图:



下面来讨论另外一种情况,当水平按照权重分割屏幕时,此时将layout_width设置为wrap_content,此时layout_weight的数值仍遵循数值越大占的屏幕越大。


那么假如layout_width设置为fill_parent呢?

这里介绍一种计算方法,这里预先设置一个变量暂时定义为queshao(缺少),例如在一个orientation为horizontal的LinearLayout中,有三个组件的layout_width都设定为了fill_parent,此时queshao=1-3 = -2(因为只有一个parent大家都想fill_parent,那么此时就缺少一个parent)

假如这三个组件的layout_weight依次为1 2 3,那么公式如下:


第一个组件:1-queshao*(1/(1+2+3))=2/3

第二个组件:1-queshao*(2/(1+2+3)) = 1/3

第三个组件:没空了~~

示例代码及运行图:



复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:id="@+id/LinearLayout1" android:layout_width="match_parent" android:layout_height="match_parent" > <TextView android:layout_weight="1" android:layout_width="fill_parent" android:layout_height="fill_parent" android:text="one" android:background="#98FB98" /> <TextView android:layout_weight="2" android:layout_width="fill_parent" android:layout_height="fill_parent" android:text="two" android:background="#FFFF00" /> <TextView android:layout_weight="3" android:layout_width="fill_parent" android:layout_height="fill_parent" android:text="three" android:background="#FF00FF" /> </LinearLayout>


未完待续。。。

最后

以上就是呆萌芒果最近收集整理的关于LinearLayout中的layout_weight属性详解的全部内容,更多相关LinearLayout中内容请搜索靠谱客的其他文章。

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

评论列表共有 0 条评论

立即
投稿
返回
顶部