最近,一个项目中,我尝试给一个merge的标签设置背景图片,结果还是没有显示出来。查了下发现merge标签原来是这样用的。
新建一个HelloWorld android项目,布局文件代码如下:
<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:paddingBottom="@dimen/activity_vertical_margin" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" tools:context=".MainActivity" > <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/hello_world" /> </RelativeLayout> 查看布局,如下:
很符合我们的预期,RelativeLayout的父元素是id/content。
当我们把RelativeLayout修改成Merge标签时:
<merge 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:paddingBottom="@dimen/activity_vertical_margin" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" tools:context=".MainActivity" > <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/hello_world" /> </merge>
查看布局如下:
你会发现merge消失了,没错,它起的作用就是这样,把里面的子布局直接附到它的父布局中。这样就省略了一个布局,会有性能提升!在Phone 的Apk中就大量使用了merge。
回到标题,就知道为什么设置背景无效了。真正渲染布局时,它是不存在的。也就是说不光是背景,任何如margin,padding的属性都将无效。
http://www.waitingfy.com/?p=745
Tags: android
745