android布局文件 merge 标签的使用

来源:互联网 发布:中国当前网络消费现状 编辑:程序博客网 时间:2024/05/16 00:49
<merge xmlns:android="http://schemas.android.com/apk/res/android">    <ToggleButton        android:id="@+id/toggleButton1"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:text="ToggleButton" />    <TextView        android:layout_width="wrap_content"         android:layout_height="wrap_content"         android:layout_marginBottom="20dip"        android:layout_gravity="center_horizontal|bottom"        android:padding="12dip"        android:background="#AA000000"        android:textColor="#ffffffff"        android:text="Golden Gate" /></merge>

1.merge布局 和FrameLayout类似,相同的效果.不同的是 merge布局只能被<include>标签包含. 或者Activity.setContentView所使用.

当LayoutInflater遇到能被其他layout用<include>包含进去,并不再另外生成ViewGroup容器,本元素也特别有用这个标签时,它会跳过它,并将<merge />内的元素添加到<merge />的父元素里. Activity能直接使用的原因是Activity的父元素是FrameLayout

2 merge 能被其他layout用<include>包含进去,并不再另外生成ViewGroup容器.就是说,会减少一层layout到达优化layout的目的


限制:

         <merge />只能作为XML布局的根标签使用
·        当Inflate以<merge />开头的布局文件时,必须指定一个父ViewGroup,并且必须设定attachToRoot为true(参看inflate(int, android.view.ViewGroup, Boolean)方法)。

原创粉丝点击