Android linearlayout中layout_weight详解
来源:互联网 发布:telnet的端口号是多少 编辑:程序博客网 时间:2024/04/29 22:59
在学习android布局过程中,发现linearlayout中的layout_weight属性有些奇怪,因此记录下来
1.先上一段代码,需要注意的是TextView中layout_width的值和layout_wwight的值
<?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="wrap_content" android:orientation="horizontal" > <TextView android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_weight="1" android:background="#FFFFFF" android:text="111" android:textSize="20sp" /> <TextView android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_weight="1" android:background="#444444" android:text="222" android:textSize="20sp" /> <TextView android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_weight="1" android:background="#666666" android:text = "333" android:textSize="20sp"/> <TextView android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_weight="1" android:background="#999999" android:text = "444" android:textSize="20sp"/> </LinearLayout>
下面是这段代码的效果图
2.改变TextView的layout_weight值
<?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="wrap_content" android:orientation="horizontal" > <TextView android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_weight="1" android:background="#FFFFFF" android:text="111" android:textSize="20sp" /> <TextView android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_weight="1" android:background="#444444" android:text="222" android:textSize="20sp" /> <TextView android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_weight="<span style="color:#FF0000;">2</span>" android:background="#666666" android:text = "333" android:textSize="20sp"/> <TextView android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_weight="<span style="color:#FF0000;">4</span>" android:background="#999999" android:text = "444" android:textSize="20sp"/> </LinearLayout>注意此处只修改了两个参数的值,下面是效果图
是不是有点奇怪呢,第三个和第四个TextView都没有显示出来
3.下面将TextView的数目减少到两个,而layout_weight属性值分别设置为1和2,会发生什么情况呢
<?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="wrap_content" android:orientation="horizontal" > <TextView android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_weight="<span style="color:#FF0000;">1</span>" android:background="#FFFFFF" android:text="111" android:textSize="20sp" /> <TextView android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_weight="<span style="background-color: rgb(255, 0, 0);">2</span>" android:background="#444444" android:text="222" android:textSize="20sp" /> </LinearLayout>
会出现什么情况呢,下面是效果图
4.然后将其中一个layout_weight的值不做设置,看看默认的会是什么样的
<?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="wrap_content" android:orientation="horizontal" > <TextView android:layout_width="match_parent" android:layout_height="wrap_content" android:background="#FFFFFF" android:text="111" android:textSize="20sp" /> <TextView android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_weight="2" android:background="#444444" android:text="222" android:textSize="20sp" /></LinearLayout>下面是效果图
看样子不写的时候layout_weight的值默认是0了
5.下面将TextView中的layout_width值设置为0,看看又会出现什么意想不到的效果呢
<?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="wrap_content" android:orientation="horizontal" > <TextView android:layout_width="0" android:layout_height="wrap_content" android:layout_weight="1" android:background="#FFFFFF" android:text="111" android:textSize="20sp" /> <TextView android:layout_width="0" android:layout_height="wrap_content" android:layout_weight="2" android:background="#444444" android:text="222" android:textSize="20sp" /> <TextView android:layout_width="0" android:layout_height="wrap_content" android:layout_weight="3" android:background="#666666" android:text = "333" android:textSize="20sp"/> <TextView android:layout_width="0" android:layout_height="wrap_content" android:layout_weight="4" android:background="#999999" android:text = "444" android:textSize="20sp"/> </LinearLayout>下面上效果图了
总结:
通过上面的几个例子可以得出以下结论
1.当LinearLayout的几个子控件的layout_width="match_parent"的时候,而且有layout_weight的时候,子控件所占的宽度与layout_weight的值成反比,layout_weight的默认值是0,即会占满整个宽度。
2.至于当子控件的layout_weight值设置的不一致时,有些子控件并没有显示,很奇怪
3.当子控件的layout_width设置为0dip的时候,每个自控件耳朵宽度与该子控件的layout_weight的值成正比。
本人也是android初学者,希望多与大家交流,学习经验。如有不当或错误之处,请指出。
0 0
- Android linearlayout中layout_weight详解
- 摘抄一篇文章android中LinearLayout中layout_weight详解
- LinearLayout中android:layout_weight的作用
- LinearLayout中android:layout_weight的作用
- 对android LinearLayout中layout_weight属性使用
- LinearLayout中android:layout_weight的作用
- Android LinearLayout布局中 layout_weight解析
- android 中layout_weight 详解
- LinearLayout的android:layout_weight
- Android LinearLayout layout_weight
- LinearLayout中layout_weight属性
- LinearLayout中使用layout_weight!!!
- LinearLayout中layout_weight属性
- android之LinearLayout中android:layout_weight属性理解
- LinearLayout中的layout_weight属性详解
- 关于linearLayout的中 android:layout_weight 的使用
- android LinearLayout布局中layout_weight的使用(一)
- android LinearLayout布局中layout_weight的使用(二)
- POJ 1936(字符串匹配)
- 【JS学习笔记】 面向对象与原型
- LeetCode 64. Minimum Path Sum
- 做为网址导航生存何在或转行
- 判断滚动滚动到底部
- Android linearlayout中layout_weight详解
- for语句引起一个死循环而引发的思考!!!
- Timer和TimerTask详解
- 87. Scramble String
- android使用接口回调把fragment把值传回到Activity
- Extjs4中的Ext.form.field.Spinner
- MyEclipse中设置智能提示
- 照着韩顺平老师模仿的坦克大战案例
- 我的vimrc配置文件