五.LinearLayout的gravity,layout_gravity和weight

LinearLayout布局时有三种属性可能会经常遇到,并且还是不好理解的,所以特意拿出来说一下。

1.gravity

1.1 gravity

gravity属性是view本身内容的位置。
例如,TextView的text,Button的text等。

1.2 属性值

  • 上下左右。
属性值效果
top将对象放在其容器的顶部,不改变其大小.
bottom将对象放在其容器的底部,不改变其大小.
left将对象放在其容器的左侧,不改变其大小.
right将对象放在其容器的右侧,不改变其大小.
start是为了兼容从左到右和从右到左的不同书写顺序的
end是为了兼容从左到右和从右到左的不同书写顺序的
  • 纵横
属性值效果
center_vertical将对象纵向居中,不改变其大小。
fill_vertical如果需要时,将对象纵向填充
center_horizontal将对象横向居中,不改变其大小
fill_horizontal如果需要时,将对象横向填充
center将对象居中,不改变其大小
fill将对象横向和纵向填充
  • 附加选项
属性值效果
clip_vertical附加选项,用于按照容器的边来剪切对象的顶部和/或底部的内容. 剪切基于其纵向对齐设置:顶部对齐时,剪切底部;底部对齐时剪切顶部;除此之外剪切顶部和底部.垂直方向裁剪
clip_horizontal附加选项,用于按照容器的边来剪切对象的左侧和/或右侧的内容. 剪切基于其横向对齐设置:左侧对齐时,剪切右侧;右侧对齐时剪切左侧;除此之外剪切左侧和右侧.水平方向裁剪

注意

  • 使用LinearLayout的时候需要注意:纵只能用横,横只能用纵。
    android:orientation=”vertical”只有水平方向的属性起作用,比如:left,right,center_horizontal。
    android:orientation=“horizontal”只有垂直方向的属性起作用,比如:top,bottom,center_vertical。
    是因为已经定义过纵横了,再次定义将无效化。

下面是例子:

gravity

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#B2EBF2">
    <TextView
        android:layout_width="300dp"
        android:layout_height="50dp"
        android:background="#4db6ac"
        android:layout_gravity="right"
        android:layout_marginTop="1dp"
        android:text="right"
        android:textSize="40sp" />

    <TextView
        android:layout_width="310dp"
        android:layout_height="50dp"
        android:background="#4db6ac"
        android:layout_gravity="center_horizontal"
        android:layout_marginTop="1dp"
        android:text="center_horizontal"
        android:textSize="40sp" />
    <TextView
        android:layout_width="300dp"
        android:layout_height="50dp"
        android:background="#4db6ac"
        android:layout_gravity="center_vertical"
        android:layout_marginTop="1dp"
        android:text="center_vertical"
        android:textSize="40sp" />
    <TextView
        android:layout_width="300dp"
        android:layout_height="50dp"
        android:background="#4db6ac"
        android:layout_gravity="fill_horizontal"
        android:layout_marginTop="1dp"
        android:text="fill_horizontal"
        android:textSize="40sp" />
    <TextView
        android:layout_width="300dp"
        android:layout_height="50dp"
        android:background="#4db6ac"
        android:layout_gravity="clip_horizontal"
        android:layout_marginTop="1dp"
        android:text="clip_horizontal"
        android:textSize="40sp" />
</LinearLayout>

2.1 layout_gravity

细心的你发现了,与前一个十分相似。它们的区别是:

  • android:gravity是自己内部的子元素
  • android:layout_gravity是控制自己在父元素的位置。

1.2 属性

属性一毛一样。。

  • 上下左右。
属性值效果
top将对象放在其容器的顶部,不改变其大小.
bottom将对象放在其容器的底部,不改变其大小.
left将对象放在其容器的左侧,不改变其大小.
right将对象放在其容器的右侧,不改变其大小.
  • 纵横
属性值效果
center_vertical将对象纵向居中,不改变其大小。
fill_vertical将对象纵向填充
center_horizontal将对象横向居中,不改变其大小
fill_horizontal将对象横向填充
center将对象居中,不改变其大小
fill将对象横向和纵向填充
  • 附加选项
属性值效果
clip_vertical附加选项,用于按照容器的边来剪切对象的顶部和/或底部的内容. 剪切基于其纵向对齐设置:顶部对齐时,剪切底部;底部对齐时剪切顶部;除此之外剪切顶部和底部.垂直方向裁剪
clip_horizontal附加选项,用于按照容器的边来剪切对象的左侧和/或右侧的内容. 剪切基于其横向对齐设置:左侧对齐时,剪切右侧;右侧对齐时剪切左侧;除此之外剪切左侧和右侧.水平方向裁剪

注意

  • 使用LinearLayout的时候需要注意:纵只能用横,横只能用纵。
    android:orientation=”vertical”只有水平方向的属性起作用,比如:left,right,center_horizontal。
    android:orientation=“horizontal”只有垂直方向的属性起作用,比如:top,bottom,center_vertical。
    是因为已经定义过纵横了,再次定义将无效化。

下面是例子:

layout_gravity

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

    <TextView
        android:layout_width="100dp"
        android:layout_height="100dp"
        android:layout_marginTop="1dp"
        android:background="#4db6ac"
        android:gravity="top"

        android:text="abca"
        android:textSize="40sp" />

    <TextView
        android:layout_width="100dp"
        android:layout_height="100dp"
        android:layout_marginTop="1dp"
        android:background="#4db6ac"
        android:gravity="bottom"
        android:text="abca"
        android:textSize="40sp" />

    <TextView
        android:layout_width="100dp"
        android:layout_height="50dp"
        android:layout_marginTop="1dp"
        android:background="#4db6ac"
        android:gravity="right"
        android:text="1"
        android:textSize="40sp" />

    <TextView
        android:layout_width="100dp"
        android:layout_height="50dp"
        android:layout_marginTop="1dp"
        android:background="#4db6ac"
        android:gravity="center_horizontal"
        android:text="1"
        android:textSize="40sp" />

    <TextView
        android:layout_width="100dp"
        android:layout_height="50dp"
        android:layout_marginTop="1dp"
        android:background="#4db6ac"
        android:gravity="fill"
        android:text="a"
        android:textSize="40sp" />


</LinearLayout>

3.layout_weight

这个属性在LinearLayout的复杂布局时可能会出现,但是大家好像都讲的不是很清楚。在此详细讲讲。
首先,这个属性是权重的意思。
比如这样

两个控件都是1:1,此时不用考虑怎么去设置具体数值值让他们平分,只需要设置两个控件android:layout_weight=”1”就行了。

但是会有一些问题,一般都是Layout_width设置的问题:

  • match_parent和wrap_content显示问题。
    当设置该属性的控件的layout_width=”warp_content”,很正确.

    但是当该属性的控件的layout_width=”match_parent”的时候,同样的代码就变成了这样。

这是为什么呢?
是因为layout_weight的属性是这样计算的:
自身长度+(子权重/子权重之和)*剩余空间
当为wrap_content的时候,计算时自身长度为0,剩余空间就是match_parent,自然就是正确的比重了。
当为match_parent的时候,计算时自身长度为match_parent,剩余空间为match_parent-(控件个数)3*match_parent=-2match_parent。那么三个的占比就为:

  1. match_parent+(1/6)*-2match_parent=4/6match_parent
  2. match_parent+(2/6)*-2match_parent=2/6match_parent
  3. match_parent+(3/6)*-2match_parent=0match_parent

最后的结果为3:1:0,所以使用match_parent就很怪异。所以Google官方给的解决方法是推荐layout_width=”0dp”或layout_height=”0dp”
这样的权重就不用像上面一样纠结了,十分的方便。

github源码

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值