近期学习了Mars老师的视频,看了十二课的有关layout_weight的讲解,就做了些总结。

layout_weight用于分配剩余的布局空间。

  1. 首先,先看段代码,它定义了两个textview控件
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#00ff00"
android:text="first" /> <TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#0000ff"
android:text="seconed"/>

剩余的布局空间就是红色部分。

2.加入layout_weight代码

 <LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#ff0000">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#00ff00"
android:text="first" /> <TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#0000ff"
android:text="seconed"/>
</LinearLayout> <LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#ff0000">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:background="#00ff00"
android:text="first" /> <TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:background="#0000ff"
android:text="seconed"/>
</LinearLayout>

code中有android:layout_weight="1"这是说每个text平分红色空间的1/2;另外从图中可以看出,两个textview平分了剩余的红色空间,需要明白的是layout_weight平分的是红色空间

PS:如果设置android:layout_weight="1"另一个为android:layout_weight="2",则一个占据红色空间的1/3,另一个占据2/3.

要想两个textview不只是占据红色的剩余空间,而是拥有相同的空间,仅需将android:layout_width设为0.另外将android:layout_weight="1"即可。

(新手,有什么不对的请指正)

最新文章

  1. C# 词法分析器(七)总结
  2. html5中的大纲
  3. [CLR via C#]21. 自动内存管理(垃圾回收机制)
  4. Lucene的Vint类型详解
  5. 【转】Java生成对应字符串的MD5密码模块
  6. oracle查看所有表及字段
  7. GDI+ 图片转存
  8. 【转】获取/设置IFRAME内对象元素的几种JS方法
  9. Python异步处理
  10. 04.redis集群+SSM整合使用
  11. Android开发学习之路--RxAndroid之简单原理
  12. 用node.js express设置路径后 子路径下的页面访问静态资源路径出问题
  13. Python入门经典案例一
  14. dedecms文档长尾关键词,根据频率先替换长尾关键词再替换主关键词
  15. Java全栈程序员之09:IDEA+GitHub
  16. session依赖cookie,如果浏览器禁用了cookie呢?
  17. C#自制Web 服务器开发:mysql免安装版配置步骤详解分享
  18. 【Python】使用geopy由地址找经纬度等信息
  19. 网卡虚拟化技术:VMDq和SR-IOV
  20. 阿里云ubuntu16.04安装beef

热门文章

  1. Awk by Example--转载
  2. selenium webdriver (python) 第三版
  3. iOS-UIButton-文字位置,字体大小,边角样式,button种类,点击事件,内容位置
  4. 阅读Nosql代码有感
  5. cereal:C++实现的开源序列化库
  6. iOS二十种超酷时尚艺术滤镜汇总【附源码】
  7. mssql server提示无权限
  8. 如何高效部署前端代码,如css,js...
  9. MyBatis中的特殊符号[20160713]
  10. 三、动态SQL语句