android layout_weight
来源:互联网 发布:大连理工大学软件学院 编辑:程序博客网 时间:2024/05/16 00:27
在网上看了一些对Layout_weight的讲解,有些说的比较片面,只列举了一种情况,然后自己通过实验和一些比较好的文章总结了一下,特此记录下来,以备以后所用。Layout_weight是线性布局,也就是LinearLayout里面用到的,下面通过实验来看这个Layout_weight的特性。
同样,Button1的weight设置为1,Button2的weight设置为2,但是效果与fill_parent的效果截然相反。运行效果如下:
1.当控件的属性android:layout_width="fill_parent"时,布局文件如下:
XML代码:
<?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="@string/str1" /> <Button android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_weight="2" android:text="@string/str2" /> </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代码:
<?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的效果截然相反。运行效果如下:
- android:layout_weight
- android:layout_weight
- android:layout_weight
- android layout_weight
- Android:layout_weight
- android:layout_weight
- android layout_weight
- Android:Layout_weight
- android:layout_weight
- Android:Layout_weight
- android;layout_weight
- Android:layout_weight
- android:layout_weight
- Android:layout_weight
- android布局---android:layout_weight
- android:layout_weight android:weightSum
- android权重android:layout_weight
- android:layout_weight android:weightSum
- JQuery获取服务器ID的办法
- jquery仿tab_panel插件
- C++ 第八周任务-字符加密
- makedepend
- SQL 2008 专业课学习,问题小结,长时间更新(初学者开始的)
- android layout_weight
- mac下开发、工作工具推荐
- Eclipse修改背景色值
- Symbian OS:MVC 设计模式在 SymbianOS 应用程序中的应用分析
- linux /dev/urandom和/dev/random的区别
- cvedetails
- select函数及其使用方法
- Android设置默认浏览器
- 关于Autorelease和RunLoop