布局ViewGroup原理解析(二)

<LinearLayout

android:layout_width=“0dp”

android:layout_height=“fill_parent”

android:background=“#DA70D6”

android:layout_weight=“2”/>

要实现第一个的1:1的效果,只需要分别把两个LinearLayout的weight改成1和1就可以了 用法归纳: 按比例划分水平方向:将涉及到的View的android:width属性设置为0dp,然后设置为android weight属性设置比例即可;类推,竖直方向,只需设android:height为0dp,然后设weight属性即可! 大家可以自己写个竖直方向的等比例划分的体验下简单用法!

[](()②weight属性详解:

当然,如果我们不适用上述那种设置为0dp的方式,直接用wrap_content和match_parent的话, 则要接着解析weight属性了,分为两种情况,wrap_content与match_parent!另外还要看 LinearLayout的orientation是水平还是竖直,这个决定哪个方向等比例划分

1)wrap_content比较简单,直接就按比例的了

在这里插入图片描述

《Android学习笔记总结+最新移动架构视频+大厂安卓面试真题+项目实战源码讲义》无偿开源 徽信搜索公众号【编程进阶路】 现代码:

<LinearLayout xmlns:android=“http://schemas.android.com/apk/res/android”

xmlns:tools=“http://schemas.android.com/tools”

android:id=“@+id/LinearLayout1”

android:layout_width=“match_parent”

android:layout_height=“match_parent”

android:orientation=“horizontal” >

<TextView

android:layout_weight=“1”

android:layout_width=“wrap_content”

android:layout_height=“fill_parent”

android:text=“one”

android:background=“#98FB98”

/>

<TextView

android:layout_weight=“2”

android:layout_width=“wrap_content”

android:layout_height=“fill_parent”

android:text=“two”

android:background=“#FFFF00”

/>

<TextView

android:layout_weight=“3”

android:layout_width=“wrap_content”

android:layout_height=“fill_parent”

android:text=“three”

android:background=“#FF00FF”

/>

2)match_parent(fill_parent):这个则需要计算了

我们写这段简单的代码:

<LinearLayout xmlns:android=“http://schemas.android.com/apk/res/android”

xmlns:tools=“http://schemas.android.com/tools”

android:id=“@+id/LinearLayout1”

android:layout_width=“match_parent”

android:layout_height=“match_parent” >

<TextView

android:layout_weight=“1”

android:layout_width=“fill_parent”

android:layout_height=“fill_parent”

android:text=“one”

android:background=“#98FB98”

/>

<TextView

android:layout_weight=“2”

android:layout_width=“fill_parent”

android:layout_height=“fill_parent”

android:text=“two”

android:background=“#FFFF00”

/>

<TextView

android:layout_weight=“3”

android:layout_width=“fill_parent”

android:layout_height=“fill_parent”

android:text=“three”

android:background=“#FF00FF”

/>

运行效果图:

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值