layout_weight的使用说明
2024-09-10 23:54:38
近期学习了Mars老师的视频,看了十二课的有关layout_weight的讲解,就做了些总结。
layout_weight用于分配剩余的布局空间。
- 首先,先看段代码,它定义了两个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"即可。
(新手,有什么不对的请指正)
最新文章
- C# 词法分析器(七)总结
- html5中的大纲
- [CLR via C#]21. 自动内存管理(垃圾回收机制)
- Lucene的Vint类型详解
- 【转】Java生成对应字符串的MD5密码模块
- oracle查看所有表及字段
- GDI+ 图片转存
- 【转】获取/设置IFRAME内对象元素的几种JS方法
- Python异步处理
- 04.redis集群+SSM整合使用
- Android开发学习之路--RxAndroid之简单原理
- 用node.js express设置路径后 子路径下的页面访问静态资源路径出问题
- Python入门经典案例一
- dedecms文档长尾关键词,根据频率先替换长尾关键词再替换主关键词
- Java全栈程序员之09:IDEA+GitHub
- session依赖cookie,如果浏览器禁用了cookie呢?
- C#自制Web 服务器开发:mysql免安装版配置步骤详解分享
- 【Python】使用geopy由地址找经纬度等信息
- 网卡虚拟化技术:VMDq和SR-IOV
- 阿里云ubuntu16.04安装beef