android--RelativeLayout布局的一些理解

RelativeLayout是最灵活的布局方式,个人比较喜欢使用。

其中上下,左右的位置主要是根据android:layout_alignParentTop、android:layout_alignParentBottom、android:layout_alignParentLeft、android:layout_alignParentRight来控制。

其中的控件的相对位置根据android:layout_below="@id/"、 android:layout_above="@id/" 、android:layout_left="@id/" 、android:layout_right="@id/" 来控制。

但是xml布局文件被解释的时候回按照布局文件的先后顺序执行!!

对比以下两种情况(只是给出了布局,显示的布局删除了):

第一种:首先设置了顶部和底部,最后设置的是中间显示部分:

<RelativeLayout 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"
    android:background="@android:color/background_dark"
    android:orientation="vertical"
    tools:context=".ChooseImageActivity" >

    <RelativeLayout
        android:id="@+id/ll_top"
        android:layout_width="match_parent"
        android:layout_height="50dp"
        android:layout_alignParentTop="true"
        android:background="@color/picture_gray"
        android:gravity="center_vertical"
        android:orientation="horizontal" >

    </RelativeLayout>


     <RelativeLayout
         android:id="@+id/rl_activity_choose_image_bottom"
        android:layout_width="match_parent"
        android:layout_height="50dp"
        android:layout_alignParentBottom="true"
        android:background="@color/picture_gray"
        android:gravity="center_vertical"
        android:orientation="horizontal">
       
    </RelativeLayout>
<LinearLayout
        android:id="@+id/ll_activity_choose_image_middle"
        android:layout_width="match_parent"
        android:layout_height="fill_parent"
        android:layout_below="@id/ll_top" 
        android:orientation="vertical"
        android:layout_centerVertical="true"
        android:layout_margin="10dp"
        android:layout_above="@id/rl_activity_choose_image_bottom">
       
    </LinearLayout>

</RelativeLayout>

此时,显示效果是如下(上中下的布局):


第二种情况:(和第一种情况相比较,设置的顺序是上、中、下)

<RelativeLayout 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"
    android:background="@android:color/background_dark"
    android:orientation="vertical"
    tools:context=".ChooseImageActivity" >

    <RelativeLayout
        android:id="@+id/ll_top"
        android:layout_width="match_parent"
        android:layout_height="50dp"
        android:layout_alignParentTop="true"
        android:background="@color/picture_gray"
        android:gravity="center_vertical"
        android:orientation="horizontal" >

    </RelativeLayout>
<LinearLayout
        android:id="@+id/ll_activity_choose_image_middle"
        android:layout_width="match_parent"
        android:layout_height="fill_parent"
        android:layout_below="@id/ll_top" 
        android:orientation="vertical"
        android:layout_centerVertical="true"
        android:layout_margin="10dp" >
       
    </LinearLayout>

     <RelativeLayout
         android:id="@+id/rl_activity_choose_image_bottom"
        android:layout_width="match_parent"
        android:layout_height="50dp"
        android:layout_alignParentBottom="true"
        android:background="@color/picture_gray"
        android:gravity="center_vertical"
        android:orientation="horizontal"

android:layout_above="@id/ll_activity_choose_image_middle">

       
    </RelativeLayout>


</RelativeLayout>

此时的显示效果是:(底部被挤掉了,显示不出来)


所以,根据以上情况的对比布局文件的解析是按照xml文件的先后顺序解析的,不是总体分析后再解析。

所以要合理使用布局。建议:先将上、下规定好,中间要显示的内容就好搞定了(特别是中间的内容较小或较大时,要让其居中显示)

注:不对的地方,请指正。谢谢

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值