小玩Android布局中的weight(权重)
来源:互联网 发布:soma聊天软件好用吗 编辑:程序博客网 时间:2024/05/09 00:44
【声明】转载请注明出处,此文出自指尖飞落的博客:http://blog.csdn.net/huntersnail
——尊重作者,知识无价,交流无限!
weight是线性布局的特有属性,控件的宽度和高度的不同,也会存在差异,下面咱们就来小玩几下!
水平效果
Example 1:将宽度设置为包裹类型wrap_content/0dp
<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="horizontal" > <Button android:id="@+id/button1" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" android:text="Button1" /> <Button android:id="@+id/button2" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="2" android:text="Button2" /></LinearLayout>
我们在布局里面设置为线性布局,横向排列,然后放置两个宽度为0dp的按钮,分别设置weight为1和2,在效果图中,我们可以看到两个按钮按照1:2的宽度比例正常排列了,这也是我们经常使用到的场景,这是时候很好理解,Button1的宽度就是1/(1+2) = 1/3,Button2的宽度则是2/(1+2) = 2/3,我们可以很清楚的明白这种情景下的占比如何计算。
Example 2:将宽度设置为包裹类型match_parent
<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="horizontal" > <Button android:id="@+id/button1" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_weight="1" android:text="Button1" /> <Button android:id="@+id/button2" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_weight="2" android:text="Button2" /></LinearLayout>
我们可以看到,在这种情况下,占比和上面正好相反,这是怎么回事呢?说到这里,我们就不得不提一下weight的计算方法了。
android:layout_weight的真实含义是:如果View设置了该属性并且有效,那么该 View的宽度等于原有宽度(android:layout_width)加上剩余空间的占比。
从这个角度我们来解释一下上面的现象。在上面的代码中,我们设置每个Button的宽度都是match_parent,假设屏幕宽度为L,那么每个Button的宽度也应该都为L,剩余宽度就等于L-(L+L)= -L。
Button1的weight=1,剩余宽度占比为1/(1+2)= 1/3,所以最终宽度为L+1/3*(-L)=2/3L,Button2的计算类似,最终宽度为L+2/3(-L)=1/3L。
Example 3:将高度设置为包裹类型wrap_content/0dp
<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" > <Button android:id="@+id/button1" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_weight="1" android:text="Button1" /> <Button android:id="@+id/button2" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_weight="2" android:text="Button2" /></LinearLayout>
Example 4:将高度设置为包裹类型match_parent
<span style="font-size:12px;"><?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" > <Button android:id="@+id/button1" android:layout_width="match_parent" android:layout_height="match_parent" android:layout_weight="1" android:text="Button1" /> <Button android:id="@+id/button2" android:layout_width="match_parent" android:layout_height="match_parent" android:layout_weight="2" android:text="Button2" /></LinearLayout></span>举例到此吗,自己多试试就知道了!
☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆转载请注明出处☞指尖飞落的博客☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆
- 小玩Android布局中的weight(权重)
- 关于 android 权重 weight在布局中的理解和使用
- Android线性布局权重Weight效果展示
- android中权重属性(weight)
- android weight(权重)的具体分析
- android之线性布局LinearLayout以及weight权重使用
- Android LinearLayout布局嵌套及权重weight的使用
- Android自适应布局(关于权重weight的使用技巧!)
- android基础 --- 权重(weight)
- Android weight权重适配
- android weight(权重值) match_parent
- Android基础:xml布局中的Layout Weight
- 剖析Android 线性布局中的权重(layout_weight)
- 剖析Android 线性布局中的权重(layout_weight)(解答疑惑)
- 剖析Android 线性布局中的权重(layout_weight)(解答疑惑)
- 相对布局LinearLayout权重weight的用法
- weight布局小技巧
- Android 布局之LinearLayout 子控件weight权重的作用详析
- How-to: effective store kafka data into hdfs via spark streaming
- jQuery插件第五十六:十七种翻页插件
- 黑马程序员——Java要点笔记——集合框架(Map)
- SElinux
- Image$$VECTOR$$Base
- 小玩Android布局中的weight(权重)
- mac上通过npm安装cordova失败
- Android 获取和设置屏幕亮度的模式和值
- listView的header的宽高的解析问题
- 阿里巴巴的菜鸟网络
- MonkeyTalk 传输协议
- JAVA更改软件标题栏默认图标的方法
- leetCode(36):Sum Root to Leaf Numbers
- jQuery DOM节点操作