Android布局细节:layout_weight的使用方法
来源:互联网 发布:php base64编码解码 编辑:程序博客网 时间:2024/05/16 09:59
1.当控件的属性android:layout_width="fill_parent"时,布局文件如下:
<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="horizontal" android:layout_width="fill_parent" android:layout_height="fill_parent"> <Button android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_weight="1" android:text="Button1" /> <Button android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_weight="2" android:text="Button2" /></LinearLayout>
在这里Button1的Layout_weight=1,Buttong2的Layout_weight=2,运行效果为:
我们看到,Button1占了2/3,Button2占了1/3。如果此时把button2的weight设置成2000,不是说Button2就消失了,而是Button1的宽度几乎占满了屏幕宽度,而屏幕最后一丝细条则是留给Button2的,已近非常小了,没有显示出来。截图如下:
得出结论:在layout_width设置为fill_parent的时候,layout_weight代表的是你的控件要优先尽可能的大,但尽可能大是有限度的,即fill_parent.
2.当控件的属性android:layout_width="wrap_content"时,布局文件如下:
<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="horizontal" android:layout_width="fill_parent" android:layout_height="fill_parent"> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_weight="1" android:text="Button1" /> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_weight="2" android:text="Button2" /></LinearLayout>同样,Button1的weight设置为1,Button2的weight设置为2,但是效果与fill_parent的效果截然相反。运行效果如下:
这时,和fill_parent正好相反,Button1的宽度占据了屏幕宽度的1/3,而Button2的宽度占据了屏幕的2/3,如果此时把Button2的weight设置为2000,按照之前理解,Button1应该小的几乎在屏幕上看不到,但是错了,实验告诉我们,当Button1的weight非常小时,也要"wrap_content",也就是要保证Button1至少能够显示。以下是Button1设置weight为2000时的运行截图:
我们看到,Button1已经足够小,但是要保证他能显示出来,因此得出结论:
在layout_width设置为wrap_content的时候,layout_weight代表的是你的控件要优先尽可能的小,但这个小是有限度的,即wrap_content.
当了解这些后,我们再设计程序时,为了能够自适应屏幕,不想给控件一个指定的宽度和高度,就可以使用这个weight属性来让它按自己比例来划分屏幕高度或者宽度了。
- Android布局细节:layout_weight的使用方法
- Android布局的layout_weight属性
- android布局---android:layout_weight
- ANDROID布局中LAYOUT_WEIGHT的作用
- android 布局中的layout_weight的作用
- Android布局学习四:layout_weight 的使用
- Android布局设计中的layout_weight的学习
- Android布局属性Layout_weight的标准解释
- Android 布局 LinearLayout 的 layout_weight 属性
- Android布局中Layout_weight的属性
- Android布局中Layout_weight的属性
- Android布局中Layout_weight的属性
- Android LinearLayout布局的layout_weight属性探究
- Android布局中Layout_weight的属性
- Android布局之layout_weight的用法
- android:layout_weight 使用方法
- Android 布局 Layout_weight 详解
- Android布局之layout_weight
- [技巧心得] IE6\7\8\9 CSS Hack
- jsp全局变量与局部变量
- POJ 1128 Channel Allocation
- 复用,灵活,复杂
- 浅谈通过各种方式java连接数据库
- Android布局细节:layout_weight的使用方法
- Spring容器的AOP编程
- Java学习从入门到精通
- 查找算法(静态查找)
- Hdu3584 Cube
- JS中substr的用法
- 2013年US News全美综合大学排名(全)
- windows7上administrator用户不见了
- 在SQL SERVER查看数据库中有哪些数据表