关于Layout_weight的布局问题
来源:互联网 发布:淘宝一元包邮 编辑:程序博客网 时间:2024/06/05 09:27
需要注意的是Layout_weight的值并不是越大其占得比重就越大!恰恰相反,如果有两个控件的属性值分别是1、2,则其屏幕分割比例应该分别是2/3、1/3。严格遵循这种比例关系的前提是两个控件的layout_width或layout_height(取决于哪个维度竞争屏幕空间)都为fill_parent。
在网上看了一些对Layout_weight的讲解,有些说的比较片面,只列举了一种情况,然后自己通过实验和一些比较好的文章总结了一下,特此记录下来,以备以后所用。Layout_weight是线性布局,也就是LinearLayout里面用到的,下面通过实验来看这个Layout_weight的特性。
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="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代码
<?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,如果此时把Button1的weight设置为2000,按照之前理解,Button1应该小的几乎在屏幕上看不到,但是错了,实验告诉我们,当Button1的weight非常小时,也要"wrap_content",也就是要保证Button1至少能够显示。以下是Button1设置weight为2000时的运行截图:
我们看到,Button1已经足够小,但是要保证他能显示出来,因此得出结论:
在layout_width设置为wrap_content的时候,layout_weight代表的是你的控件要优先尽可能的小,但这个小是有限度的,即wrap_content.
当了解这些后,我们再设计程序时,为了能够自适应屏幕,不想给控件一个指定的宽度和高度,就可以使用这个weight属性来让它按自己比例来划分屏幕高度或者宽度了。
- 关于Layout_weight的布局问题
- LinearLayout布局的Layout_weight
- LinearLayout布局的Layout_weight
- 关于android:layout_weight问题
- 关于Layout_weight的理解
- 关于Android的layout_weight
- 线性布局里面的layout_weight
- Android布局的layout_weight属性
- LinearLayout布局的layout_weight属性
- 关于android中Layout_weight问题
- linearlayout布局中layout_weight使用问题
- 有关android布局的新见解, 大致是关于layout_weight的一种用法。
- 关于android layout_weight的疑问
- 关于android LinearLayout 的layout_weight
- 关于layout_weight属性的使用
- 关于android:layout_weight的含义
- 布局文件中layout_weight的作用
- ANDROID布局中LAYOUT_WEIGHT的作用
- pager-taglib分页方案一
- 封装志复刻版 PDF[完整版,去除加密]
- 非归档模式下也可能能够recover?
- Linux系统守护进程xinetd与独立守护进程standalone
- 黑马程序员——面向对象程序设计
- 关于Layout_weight的布局问题
- 十一周——回文数
- 【黑马程序员】 java笔记——基础加强
- Linux Oracle 启动过程(个人)
- matlab2013a 中 mex代码的升级问题
- 十一周——回文数(bool版)
- sdk manager制作avd,cpu/abi对应的是灰色的
- android init.rc 语法分析
- 函数指针的简单应用2