Android中View绘制优化二一---- 使用include /标签复用布局文件
本文原创, 转载请注明出处:http://blog.csdn.net/qinjuning
译二:
使用<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>
以“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
Android中View绘制优化二一---- 使用include /标签复用布局文件相关推荐
- Android优化——UI优化(二) 使用include标签复用布局
使用include标签复用布局 - 1.include标签的作用 假如说我下图的这个布局在很多界面都用到了,我该怎么办?每个页面都写一遍的话,代码太冗余,并且维护难度加大. <LinearLay ...
- Android中View绘制各种状态的背景图片原理深入分析以及StateListDrawable使用
/* Call this to force a view to update its drawable state. This will cause drawableStateChanged to b ...
- Android中View绘制流程以及invalidate()等相关方法分析
...
- android view 绘制过程,深入理解Android中View绘制的三大流程
前言 最近对Android中View的绘制机制有了一些新的认识,所以想记录下来并分享给大家.View的工作流程主要是指measure.layout.draw这三大流程,即测量.布局和绘制,其中meas ...
- Android中View绘制流程分析
创建Window 在Activity的attach方法中通过调用PolicyManager.makeNewWindo创建Window,将一个View add到WindowManager时,Window ...
- Android中View绘制流程
2019独角兽企业重金招聘Python工程师标准>>> 整个View树的绘图流程是在ViewRoot.java类的performTraversals()函数展开的,该函数做的执行过程 ...
- Android产品研发(二十一)--Android中的UI优化
转载请标明出处:一片枫叶的专栏 上一篇文章中我们讲解了Android产品研发过程中的代码Review.通过代码Review能够提高产品质量,增强团队成员之间的沟通,提高开发效率,所以良好的产品开发迭代 ...
- Android 中View的绘制机制源代码分析 三
到眼下为止,measure过程已经解说完了,今天開始我们就来学习layout过程.只是在学习layout过程之前.大家有没有发现我换了编辑器,哈哈.最终下定决心从Html编辑器切换为markdown编 ...
- Android 自定义View绘制的基本开发流程 Android自定义View(二)
1 View绘制的过程 View的测量--onMeasure() View的位置确定--onLayout() View的绘制--onDraw() 2 View的测量--onMeasure() Andr ...
最新文章
- 不是python中用于开发用户界面的第三方库-Python计算生态习题(50题)
- python自动化_Python报表自动化
- 让Eclipse启动时显示选择workspace的对话框
- EmacsLisp学习
- html5移动页面自适应手机屏幕大小,移动页面自适应手机屏幕的方法
- Houdini vex用勾股定理推算椭圆运动轨迹
- S7 200 smart模拟量应用介绍
- Qt模仿网易云黑胶片转动动画
- word设置行距18磅
- 一起来云赏月把!three.js实现vr赏月!
- 蚂蚁金服推出 BaaS 平台:巨头角逐之下,商业机会正快速来临
- FinalShell密码找回
- oracle 认识点概述(一)
- emc re 整改 超标_RE102测试中单点超标且高频有杂散如何整改?
- 【Python】cannot import name ‘ParserError‘ from ‘dateutil.parser‘
- 抗混叠滤波及折叠频率
- 矿大2019年 微机原理与接口考试题型
- B端产品经理和C端产品经理,做哪个更好?
- 优思学院|什么是六西格玛?一文解答你对六西格玛最常见的疑问
- sqlserver excel导入数据时有null,为空值