在android布局中使用include和merge标签
2016-08-17 18:08
591 查看
在android布局中使用include和merge标签
在我们开发android布局时,经常会有很多的布局是相同的,这个时候我们可以通过和标签实现将复杂的布局包含在需要的布局中,减少重复代码的编写。
1.创建一个可以重复使用的布局:
如下代码描述在应用中每个acitivity都出现的顶栏titlebar.xml<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width=”match_parent” android:layout_height="wrap_content" android:background="@color/titlebar_bg"> <ImageView android:id="@+id/title" android:layout_width="wrap_content" android:layout_height="wrap_content" android:src="@drawable/gafricalogo" /> </FrameLayout>
上面的根布局(root view)即frameLayout会出现在之后插入的地方。
2. 使用标签:
在应用中的一个activity的布局中顶栏就是如上的布局,那么我们就可以include上面的titlebar.xml达到复用的效果,布局代码如下
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width=”match_parent” android:layout_height=”match_parent” android:background="@color/app_bg" android:gravity="center_horizontal"> <include android:id="@+id/new_title" layout="@layout/titlebar"/> <TextView android:layout_width=”match_parent” android:layout_height="wrap_content" android:text="@string/hello" android:padding="10dp" /> </LinearLayout>
通过取得元素的id,我们可以修改include标签中元素的属性,下面的例子为在actiivty中修改titlebar中的图片:
private View mTitleBar = null; private ImageView mTitleImageView = null; mTitleBar = findViewById(R.id.new_title); mTitleImageView = (ImageView)mTitleBar.findViewById(R.id.title); mTitleImageView.setImageResource(R.drawable.logo);
在使用include标签时,我们可以覆写插入布局root view的属性(所有的android:layout_*属性)
<include android:id=”@+id/news_title” android:layout_width=”match_parent” android:layout_height=”match_parent” layout=”@layout/title”/>
如果需要覆写插入布局root view的属性,则必须制定android:layout_width和android:layout_height这两个属性以使其它的覆写属性生效。
3. 使用<merge/>标签
merge标签用来消除我们在include一个布局到另一个布局时所产生的冗余view group。比如现在很多布局中会有两个连续的Button,于是我们将这两个连续的Button做成可复用布局(re-usable layout)。在使用include标签时我们必须先将这两个Button用一个view group比如LinearLayout组织在一起然后供其它布局使用,如果是include的地方也是LiearLayout就会造成有两层连续的LiearLayout,除了降低UI性能没有任何好处。这个时候我们就可以使用标签作为可复用布局的root view来避免这个问题。
<merge xmlns:android="http://schemas.android.com/apk/res/android"> <Button android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="@string/add"/> <Button android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="@string/delete"/> </merge>
当我们用标签复用上述代码时,系统会忽略merge元素,直接将两个连续的Button放在标签所在处。
相关文章推荐
- 【转】在Android布局中使用include和merge标签
- Android中使用include标签和merge标签重复使用布局
- 在android布局中使用include和merge标签
- [Android developers blog 学习笔记]android 布局优化:标签include和merge的使用
- Android中使用include标签和merge标签重复使用布局
- Android布局优化几个重要标签的使用include、merge、ViewStub、requestFocus
- 在android布局中使用include和merge标签
- Android中使用include标签和merge标签重复使用布局
- 在android布局中使用include和merge标签
- 【转】在Android布局中使用include和merge标签
- 在android布局中使用include和merge标签
- Android里merge和include标签的使用
- Android里merge和include标签的使用
- Android UI 优化 使用<include/>和 <merge />标签
- Android里merge和include标签的使用
- android UI进阶之布局的优化---使用include和merge
- Android里merge和include标签的使用
- Android里merge和include标签的使用
- android_应用开发之(使用<include>标签重用布局)
- 【layout】Android中merge和include标签的使用