关于RelativeLayout的使用的注意事项

 RelativeLayout是android为android程序员提供的ViewGroup之一,相比其他layout manager,使用RelativeLayout可以减少view树的深度(nested views),可以增加绘制的效率和比如我们常常使用的findViewById的效率,因为RelativeLayout提供了强大的定位功能,但是在某些情况下RelativeLayout无法达到想要的功能。比如在RelativeLayout的width是wrap_content的情形下,使用alignParentRight时,出现奇怪的现象,某些View占了所有的宽度。或者在height是wrap_content,子view是alignParentBottom的情形下。对此Google 稳当有相关解释。 Android RelativeLayout,在链接中有这样一段话: Note that you cannot have a circular dependency between the size of the RelativeLayout and the position of its children. For example, you cannot have a RelativeLayout whose height is set to WRAP_CONTENT  and a child set to  ALIGN_PARENT_BOTTOM .。大意是不能有相互依赖的属性或者行为,比如RelativeLayout是WRAP_CONTENT(自适应内容),但是子view却是ALIGN_PARENT_BOTTOM.本身因为RelativeLayout的高度由子view来确定,但是子view却依赖RelativeLayout来确定layout.所以在使用RelativeLayout的时候避免这种相互依赖的情形。

你可能感兴趣的:(android)