译二:
使用<include />标签复用布局文件
翻译地址:http://developer.android.com/training/improving-layouts/reusing-layouts.html#Merge
尽管Android通过内置了各种各样的控件提供了微小、可复用的交互性元素,也许你需要复用较大的
组件 ----某些特定布局文件 。为了更有效率复用的布局文件,你可以使用<include />以及<merge />
标签将其他的布局文件加入到当前的布局文件中。
复用布局文件是一种特别强大的方法,它允许你创建可复用性的布局文件。例如,一个包含“Yse”or“No”的
Button面版,或者是带有文字说明的 Progressbar。复用布局文件同样意味着你应用程序里的任何元素都能从
繁杂的布局文件提取出来进行单独管理,接着你需要做的只是加入这些独立的布局文件(因为他们都是可复用地)。
因此,当你通过自定义View创建独立的UI组件时,你可以复用布局文件让事情变得更简单。
1、创建一个可复用性的布局文件
如果你已经知道复用布局的”面貌”,那么创建、定义布局文件( 命名以”.xml”为后缀)。例如,这里是一个来自
G-Kenya
codelab 的布局文件,定义了在每个Activity中都要使用的一个自定义标题 (titlebar.xml):由于这些
可复用性布局被添加至其他布局文件中,因此,它的每个根视图(root View)最好是精确(exactly)的。
<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:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/gafricalogo" />
</FrameLayout>
2、使用<include />标签
在需要添加这些布局的地方,使用<include />标签 。 例如,下面是一个来自G-Kenya codelab的布局文件,
它复用了上面列出的“title bar”文件, 该布局文件如下:
<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 layout="@layout/titlebar"/>
<TextView android:layout_width=”match_parent”
android:layout_height="wrap_content"
android:text="@string/hello"
android:padding="10dp" />
...
</LinearLayout>
你也可以在<include />节点中为被添加的布局文件的root View定义特别标识,重写所有layout参数即可(任何
以“android:layout_”为前缀的属性)。例如:
<include android:id=”@+id/news_title”
android:layout_width=”match_parent”
android:layout_height=”match_parent”
layout=”@layout/title”/>
3、使用<merge />标签
当在布局文件中复用另外的布局时, <merge />标签能够在布局层次消除多余的视图元素。例如,如果你的
主布局文件是一个垂直地包含两个View的LinearLayout,该布局能够复用在其他布局中,而对任意包含两个View的
布局文件都需要一个root View(否则, 编译器会提示错误)。然而,在该可复用性布局中添加一个LinearLayout
作为root View,将会导致一个垂直的LinearLayout包含另外的垂直LinearLayout。内嵌地LinearLayout只能减缓
UI效率,其他毫无用处可言。
该复用性布局利用.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="horizontal">
<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"/>
</LinearLayout>
为了避免冗余的布局元素,你可以使用<merge />作为复用性布局文件地root View 。例如:
使用<merge />标签的布局文件:
<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>
现在,当你添加该布局文件时(使用<include />标签),系统忽略< merge />节点并且直接添加两个Button去
取代<include />节点。
另外的,按需加载View视图 ,请看:
http://developer.android.com/training/improving-layouts/loading-ondemand.html
如何使ListView流畅滑动 ,请看:
http://developer.android.com/training/improving-layouts/smooth-scrolling.html
分享到:
相关推荐
ait descriptors parsing<br/><br/>2003.01.19:<br/> - fixed bug for ait section parsing<br/> - started code clean up and consolidation for easy porting<br/><br/>2003.01.13:<br/> - added initial ait ...
For making animations more real, I created another project named Android Easing Functions which is an implementations of easing functions on Android. So, we need to dependent that project. Step 1 ...
使用Maven POM editor打开项目中的pom.xml文件,选择Dependencies,在Dependencies栏目点击Add进行,首先弹出一个搜索按钮,例如输入spring-web,就会自动搜索关于spring-web相关的jar包,我们选择3.0.5版本的spring...
下载cors-filter-1.7.jar,java-property-utils-1.9.jar这两个库文件,放到lib目录下。(可在 http://search.maven.org上查询并下载。)工程项目中web.xml中的配置如下: [html] view plaincopy在CODE上查看代码...
<view class="container"> <!--头部 <view class='header'> <navigator class='on' hover-class='on'>推荐</navigator> <navigator hover-class='on'>会议</navigator> <navigator hover-class='on'>...
<br/>一、AnyView(网络警)标准版:<br/> 通过局域网内任何一台计算机监视、记录、控制其他计算机的上网行为;自动拦截、管理、备份局域网内所有电脑收发E-mail、浏览的网页、聊天行为、游戏行为、流量监视和流量...
<tr><td colspan="2"><jsp:include page="view/AdminTop.jsp" /></td></tr> <tr bgcolor="#F0EAED"> <td width="180" valign="top"><jsp:include page="view/AdminLeft.jsp"/></td> <td width="598" align=...
微信小程序 超精简商城模板 首页wxml 部分代码如下<!--index.wxml-->... <view class="section group-sku"></view> </scroll-view> </view> <!--<include src="../../common/footer/footer.wxml"/>-->
<artifactId>easypoi-base</artifactId> <version>3.0.3</version> </dependency> <!--easypoi-web 耦合了spring-mvc 基于AbstractView,极大的简化spring-mvc下的导出功能--> <dependency> <groupId>...
Qt5开发及实例,实例CH1601,PieView控件销售数量浏览,登录、列表数据VIEW,销售系统例程 <日销售清单> <日期 date="2017-02-24"> <时间 time="13:36"> <厂家>上海大众</厂家> <品牌>帕萨特</品牌> <报价>27</...
<description>Spring公共配置文件</description> <!-- mes 的數據庫 --> <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource" destroy-method="close"> <property name="driverClass" ...
支持读取excel封装到实体,直接使用输出流导出,使用模版导出,具体使用方法 见jar 中的README.md。源码地址:View on GitOsc https://gitee.com/modoumama/modoumama-excel-utils maven添加 <repositories> ...
<view slot="header"> <header-navbar title="个人信息"></header-navbar> </view> <!-- 关键代码 start --> <view slot="scroll" class="content"> <view class="row"> <view class="text1">头像:</view> ...
将多个集合使用一个<iterator/>标签完成迭代。 generatot标签:将指定的字符串按照规定的分隔符分解成多个子字符串。 merge标签:将多个集合拼接在一起。 subset标签:获取某个集合的子集合。 sort标签:多指定...
title=redirect" open-type="redirect" hover-class="other-navigator-hover" style=" text-align: right" style="font-size:20px">下载当前文件</navigator> <view class="btn-area" style=" text-align: right" > ...
<div class="user_handu_menu"> <ul> <li style="color:#fff;"> 韩风快时尚领先品牌 </li> <li style="background:#333;"> <a href="http://handuyishe.jd.com/" target="_blank"><strong>...
<SendTo>~/news_view.aspx?id=$1</SendTo> </RewriterRule> </Rules> </RewriterConfig> <system.web> <httpHandlers> <add verb="*" path="*.aspx" type="URLRewriter.RewriterFactoryHandler, URLRewriter...
<%@ include file="tags.jsp"%> <f:view> <h:form id="MyForm" enctype="multipart/form-data" > <h:messages globalOnly="true" styleClass="message"/> <h:panelGrid columns="3" border="0" cellspacing="5"> ...
<application android:icon="@drawable/restaurant_icon" android:label="@string/app_short_name" android:name="RestaurantFinderApplication" android:allowClearUserData="true" android:theme="@android:...
<property name="viewClass" value="org.springframework.web.servlet.view.JstlView" /> <!-- 匹配jsp文件下面的所有.jsp的页面 --> <property name="prefix" value="/jsp/" /> <property name="suffix" ...