关于Android相对布局中的四个属性:layout_alignTop,layout_alignBottom,layout_above,layout_below的分析

      前几天想要实现仿微信的界面,在使用相对布局时用到下面四个属性,分别是android:layout_alignTop,android:layout_alignBottom,android:layout_above,android:layout_below四个属性。首先来分析这4个属性:

// 相对于给定ID控件

android:layout_above 将该控件的底部置于给定ID的控件之上;

android:layout_below 将该控件的底部置于给定ID的控件之下;

android:layout_alignTop        将该控件的顶部边缘与给定ID的顶部边缘对齐;

android:layout_alignBottom   将该控件的底部边缘与给定ID的底部边缘对齐;

在使用过程中需要实现的是这个效果:



中间是一个Fragment,我们需要为这个Fragment确定位置,下面贴一下布局文件:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
      <include 
        android:id="@+id/actionbar_my"
        layout="@layout/main_title"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        />

     <android.support.v4.view.ViewPager
        android:id="@+id/viewpager"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true"
        android:focusableInTouchMode="true"
        android:layout_centerVertical="true"
        android:layout_above="@+id/bottombar_my"
        android:layout_below="@+id/actionbar_my"
        />
       <!--  android:layout_alignBottom="@+id/actionbar_my"
        android:layout_alignTop="@+id/bottombar_my"  -->
     <include 
         android:id="@+id/bottombar_my"
        layout="@layout/bottom_change_module"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="false"
        android:layout_alignParentBottom="true"
        />

</RelativeLayout>

很明显的三段结构,三段的layout_height都是通过wrap_content来确定的,在上下两个部分分别使用layout_alignParentTop和layout_alignParentBottom确定位置后,中间部分确定位置就变得极为关键了,看代码中我在用两种方式布局,如代码中使用layout_above和代码中layout_below来确定位置。那么当使用layout_alignBottom和layout_alignTop来布局的时候效果如下:


中间部分丢失,看不到了,那么两种情况结合起来看看,效果如下:


抓狂了,为什么不出现中间部分的布局那?

关键在于alignTop 以及alignBottom是和其他控件的顶部(底部)对齐,也就是说,是中间控件去实现和上部控件顶部对齐,和下部控件底部对齐,基本上等于match_parent。但是above和below是让中间控件的顶部和上部控件的底部对齐,和下部控件的顶部对齐,确定自己的位置,这个位置才是我们真正需要的位置。但有个问题想不明白,就是四个属性同时使用,还是没有我们想要的效果啊!这个说实话我无法解释,百度google并未找到啊!

在这里记录一下,在以后的学习工作中注意这个问题!

  • 6
    点赞
  • 17
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值