android关于LinearLayout的坑
来源:互联网 发布:银河证券手机炒股软件 编辑:程序博客网 时间:2024/06/08 04:00
以前开始学的时候
我们都知道如果LinearLayout的布局方向有两种
1.horizontal(水平方向布局)
2.vertical(垂直方向布局)
如果LinearLayout的布局方向是horizontal,内部的控件就绝对不能将宽度指定为match_parent,因为这样的话,单独一个控件就会将整个水平方向占满,其他的控件就没有可以放置的位子了,同样道理如果LinearLayout的布局方向是vertical,内部的的控件就不能将高度指定为mach_parent
这段话看似很好理解,我也自以为是的很快就理解了,实则不然
今天写代码突然发现一个“BUG”
<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:layout_width="wrap_content" android:layout_height="wrap_content" android:text="1"> </Button> <Button android:layout_width="match_parent" android:layout_height="wrap_content" android:text="2"> </Button></LinearLayout>按道理说,上面的代码由于我的第二Button指定了layout_width="match_parent"所以只能看到第二个Button,因为第一个Button被第二个Button挤掉了
但是出来的效果却让我大吃一惊,如下图
我勒个去。。。应该是以下的代码才会出现上图的效果啊
<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:layout_width="wrap_content" android:layout_height="wrap_content" android:text="1"> </Button> <Button android:layout_width="0dp" android:layout_weight="1" android:layout_height="wrap_content" android:text="2"> </Button></LinearLayout>无奈,让我冥思苦想一个晚上
终于开窍
第一段代码导致这种效果的合理解释是
因为第一个按钮已经占据了一部分空间,第二个按钮再设置layout_width="match_parent"的时候,只会挤掉排列在第二个按钮之后的控件控件,不会挤掉之前控件的空间,于是,抱着这个想法我又开始新一轮的尝试
<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:layout_width="wrap_content" android:layout_height="wrap_content" android:text="1"> </Button> <Button android:layout_width="match_parent" android:layout_height="wrap_content" android:text="2"> </Button> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="3"> </Button></LinearLayout>果不其然,上面代码的效果与图1一毛一样,看来我的推测是正确的
结论:当时LinearLayout布局时,最后一个控件指定是laytout_width="match_parent"就会占满剩余的所有空间,但是如果后期再想在这个所谓的“最后一个控件”后面再加一个控件,这个控件是不会显示出来的,还是按照标准的写法,把layout_width或者layout_height的值设置为“0dp”再加上layout_weight="1"的这种写法比较保险,不要为了一时偷懒导致后患无穷
阅读全文
0 0
- android关于LinearLayout的坑
- 关于android LinearLayout 的layout_weight
- Android中关于LinearLayout均匀分布的问题
- 关于android LinearLayout的比例布局
- 关于android LinearLayout的比例布局
- 关于LinearLayout属性android:layout_marginRight的错误
- 关于linearLayout的中 android:layout_weight 的使用
- 关于Android的LinearLayout的addView方法解析
- 初学Android学到的关于LinearLayout的知识
- LinearLayout的android:layout_weight
- Android的LinearLayout布局
- android的linearlayout
- android的LinearLayout杂记
- Android的LinearLayout 的internal
- Android 之LinearLayout的属性
- android布局LinearLayout的使用
- android LinearLayout的weight问题
- android linearlayout 的gravity属性
- Struts2下载及配置
- 工具使用:centOS上的防火墙
- linux网络编程之套接字编程
- 设计模式之外观模式
- leetCode--linked-list-cycle-ii
- android关于LinearLayout的坑
- 冒泡排序
- Django中中间件系列之十四
- Android studio设置activity主题程序崩溃解决办法
- Servlet中的文件上传
- java中重载与重写的区别
- Eclipse中修改注释中@author
- MyBatis集成log4j
- Android NDK环境搭建