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文件的先后顺序解析的,不是总体分析后再解析。

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

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

更多相关文章

  1. android logo:内核、android开机动画
  2. Android(安卓)实现显示文字的Gallery
  3. android 资源文件中的符号含义与说明
  4. 深度解析Android中字体设置
  5. Android图片堆叠效果实现
  6. Android控件之TextView(显示文本框控件)
  7. Android(安卓)Edittext获取焦点后,弹出的软键盘显示搜索、发送、
  8. 初学Android,Android的项目结构(二)
  9. Android(安卓)Button Maker(在线生成android shape xml文件的工具

随机推荐

  1. Android ViewDragHelper(1)
  2. Python实现的基于ADB的Android远程工具
  3. android 类型转换 工具函数
  4. Eclipse 重装Android ADT 问题~解决方案-
  5. android的优点与缺点
  6. Android中内存优化
  7. Android(安卓)JNI开发工具篇(1)-开发环境
  8. [置顶] Android异步处理系列文章索引
  9. android新建工程R报错或者R cannot be re
  10. android之XmlResourceParser类使用实例