Android学习笔记(二):layout_weight的解读
2015-11-25 12:05
344 查看
1、layout_weight:平分父容器的空间
如图:
layout_weight是,先计算设置的宽度,然后再平分剩下的宽度;
上述代码,3个text都为匹配父类,假设父类width为480,那就是先设置3个text宽为480dp,然后剩下的width为480-480*3=(-480)*2
然后再平分(-480)*2;即使是负的也能平分
text1:480+(-480)*2*(1/5)=480*(3/5)
text2:480+(-480)*2*(2/5)=480*(2/5)
text3:480+(-480)*2*(2/5)=480*(2/5)
2、layout_这个前缀的作用:如果加了layout_,就是由父容器来设置对应的属性;如果不加,就是设置本身内部的属性
eg:layout_gravity:设置其在父容器中的布局位置
gravity:设置本身内部东西的布局位置
<LinearLayout android:layout_width="match_parent" android:layout_height="match_parent"> <TextView android:id="@+id/text1" android:text="111111111111111" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_weight="1" android:background="#ff782d2d" android:autoLink="all" /> <TextView android:id="@+id/text2" android:text="2" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_weight="2" android:background="#ff1c7824" android:autoLink="all" /> <TextView android:id="@+id/text3" android:text="3" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_weight="2" android:background="#ff192278" android:autoLink="all" /> </LinearLayout>
如图:
layout_weight是,先计算设置的宽度,然后再平分剩下的宽度;
上述代码,3个text都为匹配父类,假设父类width为480,那就是先设置3个text宽为480dp,然后剩下的width为480-480*3=(-480)*2
然后再平分(-480)*2;即使是负的也能平分
text1:480+(-480)*2*(1/5)=480*(3/5)
text2:480+(-480)*2*(2/5)=480*(2/5)
text3:480+(-480)*2*(2/5)=480*(2/5)
2、layout_这个前缀的作用:如果加了layout_,就是由父容器来设置对应的属性;如果不加,就是设置本身内部的属性
eg:layout_gravity:设置其在父容器中的布局位置
gravity:设置本身内部东西的布局位置
相关文章推荐
- Android各种包的功能预览
- android
- Android TextView与html的结合使用 一个控件显示多种文字
- Android监听键盘显示和隐藏
- Android基于ViewPager Fragment实现选项卡
- 【转载】Android端百度地图API使用详解
- Android Studio新手经验
- Android 使用viewpager实现无限循环(定时+手动)
- 【Android】Fragment的生命周期详解
- Android应用插件式开发解决方法
- Android中常见的热门标签的流式布局的实现——云标签
- Android:Fresco体验(一):Android Studio导入Fresco
- 【Android】边框旁的文字实现模糊效果
- A2DP Sink, AVRCP Controller and HFP Client in Android L
- 优化Android Studio/Gradle构建
- Android工程项目解析
- 关于Android程序入口的猜想
- Android studio 上获取应用签名文件(keystore文件)的MD5值
- Android 中退出程序的几种方法
- 理解Android系统的进程间通信原理(二)----RPC机制