Android RelativeLayout padding 的一个坑?

记录最近无意间使用RelativeLayout的padding top属性时,发现的一个坑。

正常的ViewGroup 控件

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

    <FrameLayout
        android:layout_width="match_parent"
        android:layout_height="80dp"
        android:layout_gravity="center"
        android:background="@android:color/black"
        android:paddingTop="40dp">

        <ImageView
            android:layout_width="match_parent"
            android:layout_height="10dp"
            android:layout_gravity="center"
            android:layout_marginStart="33dp"
            android:background="@android:color/white"/>

        <View
            android:layout_width="match_parent"
            android:layout_height="2dp"
            android:background="@android:color/holo_green_dark" />

    </FrameLayout>
</FrameLayout>

这些普通正常的布局得到的效果如下:

这里写图片描述

可以看到加了padding top 之后内容的居中为在除去padding的真实内容显示区域内居中,这是普通正常的ViewGroup控件。

特立独行的RelativeLayout

将上述的内层FrameLayout替换为RelativeLayout,改改数值方便看到效果:

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.example.jokerlee.androidlsample.Main2Activity">

    <RelativeLayout
        android:layout_gravity="center"
        android:layout_width="match_parent"
        android:layout_height="80dp"
        android:background="@android:color/black"
        android:paddingTop="30dp">

        <ImageView
            android:layout_width="match_parent"
            android:layout_height="20dp"
            android:layout_centerVertical="true"
            android:layout_marginStart="33dp"
            android:background="@android:color/white"/>

        <View
            android:layout_width="match_parent"
            android:layout_height="2dp"
            android:background="@android:color/holo_green_dark"/>

        <ImageView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentEnd="true" />

    </RelativeLayout>

</FrameLayout>

效果如下:
这里写图片描述

其居中稳稳的把持在了整个ViewGroup高度的中央,其padding top的部分在居中时完全没有用,坑爹了word RelativeLayout。

PS:博主是在使用顶部RelativeLayout android:fitsSystemWindows=”true”属性时,恰好就在该RelativeLayout内使用了居中属性,一直在怀疑是fitsSystemWindows有使用限制,没成想是被RelativeLayout坑了。。。。。。

  • 2
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值