LinearLayout(线性布局)

一.我们屏幕适配的使用的比较多的就是LinearLayout的weight(权重属性)
1.常用属性

属性使用
orientation布局中组件的排列方式,有horizontal(水平),vertical(竖直,默认)
gravity控制组件所包含的子元素的对齐方式,可多个组合,如(left,buttom)
layout_gravity控制该组件在父容器里的对其方式
layout_width布局的宽度,通常不直接写数字的,用wrap_content(组件实际大小),fill_parent或者match_parent填满父容器
layout_height布局的高度,通常不直接写数字的,用wrap_content(组件实际大小),fill_parent或者match_parent填满父容器
id为该组件设置一个资源id,在java文件中可以通过findViewById(id)找到该组件
background为该组件设置一个背景图片,或者直接哟弄颜色覆盖

2.Weight(权重):用来等比例划分区域
最简单的用法:要等比例划分,分谁,谁为0,weight按比例即可
3.divider分割线:该属性用于为LinearLayout设置分割线图片,通过showDividers来设置分割线的所在位置,有四个可选值none,middle,begining,end
(1)divider:为LinearLayout设置分割线的图片
(2)showDividers:设置分割线所在的位置,有四个可选值:none,middle,begining,end
(3)dividerPadding:设置分割线的padding
二.weight(权重)属性详解
1.最简单的用法
在这里插入图片描述在这里插入图片描述
实现代码:

<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@id/myActionBar"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="horizontal">
<LinearLayout
    android:layout_width="0dp"
    android:layout_height="fill_parent"
    android:background="@color/colorpaper"
    android:layout_weight="1"/>
<LinearLayout
    android:layout_width="0dp"
    android:layout_height="fill_parent"
    android:background="@color/colorgreen"
    android:layout_weight="2"/>
</LinearLayout>

要实现第一个的1:1的效果,只需要分别把两个LinearLayout的weight改成1和1就可以了
用法归纳: 按比例划分水平方向:将涉及到的View的android:width属性设置为0dp,然后设置为android weight属性设置比例即可;竖直方向,只需设android:height为0dp,然后设weight属性即可
2.weight属性详解
当然,如果我们不适用上述那种设置为0dp的方式,直接用wrap_content和match_parent的话, 则要接着解析weight属性了,分为两种情况,wrap_content与match_parent,另外还要看 LinearLayout的orientation是水平还是竖直,这个决定哪个方向等比例划分
(1)wrap_content比较简单,直接就按比例
在这里插入图片描述

<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@id/myActionBar"
    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="@color/colorgreen"/>
<TextView
    android:layout_weight="2"
    android:layout_width="wrap_content"
    android:layout_height="fill_parent"
    android:text="two"
    android:background="@color/coloryellowr"/>
<TextView
    android:layout_weight="3"
    android:layout_width="wrap_content"
    android:layout_height="fill_parent"
    android:text="three"
    android:background="@color/colorpaper"/>
</LinearLayout>

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

<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@id/myActionBar"
    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="@color/colorgreen"/>
<TextView
    android:layout_weight="2"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:text="two"
    android:background="@color/coloryellowr"/>
<TextView
    android:layout_weight="3"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:text="three"
    android:background="@color/colorpaper"/>
</LinearLayout>

比例1:1:1在这里插入图片描述
有疑问说three不见了,比例从1:2:3编程了2:1:0 ,这个时候就需要去计算了
个个都是fill_parent,但是屏幕只有一个啦,那么1 - 3 = - 2 fill_parent
所以最后的结果是:one占了两份,two占了一份,three什么都没有了,还懵的话我们可以多举几个例子
在这里插入图片描述
比例2:3:4
在这里插入图片描述

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值