`
alp1014
  • 浏览: 25369 次
  • 来自: 北京
社区版块
存档分类
最新评论

Android 布局文件的性能--使用include标签重用Layout---转

阅读更多
Re-using Layouts with <include/>

尽管Android提供了很多种小的组件可以重用,我们还需要自定义一些稍微复杂一点的小组件进行重用。我们可以使用<include/> and <merge/> 标签来对当前的layout嵌入一些其他的layout.

在创建一个稍微复杂一点的layout时,重用layout是个很给力的方法。比如我们需要一个YES/NO的控制栏,包含文字提示的Progress bar。这意味着我们可以在很多地方重用那些自定义的layout.
Create a Re-usable Layout [创建一个可重用的Layout]
如果你已经知道哪些组件是会重用的,我们可以创建一个XML并且定义这个layout。
例如:下面定义了一个需要在每个Activity都需要显示的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:layout_width="wrap_content"  
               android:layout_height="wrap_content"   
               android:src="@drawable/gafricalogo" />  
</FrameLayout>  


Use the <include> Tag [使用<include>标签]
下面示例了一个包含了titlebar控件的布局:
<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里面的属性,例如:
<include android:id=”@+id/news_title”  
         android:layout_width=”match_parent”  
         android:layout_height=”match_parent”  
         layout=”@layout/title”/>  

Use the <merge> Tag [使用<merge>标签]
某些时候,自定义可重用的布局包含了过多的层级标签,比如我们需要在LinearLayout里面嵌入一个重用的组件,而恰恰这个自定义的可重用的组件根节点也是LinearLayout,这样就多了一层没有用的嵌套,无疑这样只会拖慢程序速度。而这个时候如果我们使用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平级。
分享到:
评论

相关推荐

    使用include实现布局(layout)复用

    我们是给这些个Activity布局文件都统一加上一样的布局代码,但是维护起来很麻烦,修改不方便,有没有一种类似于编程语言的include语法呢?答案是有的,但是sdk的demo并没有说出使用方法,但这并不说明不好使用,其实...

    Android代码-react-native-zoom-layout

    react-native-zoom-layout Getting started $ npm install react-native-zoom-layout --save Mostly automatic installation ...Append the following lines to android/settings.gradle: include ':react-na

    tesseract-3.02.02-win32-lib-include-dirs(源文件).zip

    tesseract-3.02.02-win32-lib-include-dirs(SDK) tesseract-3.02.02-win32-lib-include-dirs(SDK) tesseract-3.02.02-win32-lib-include-dirs(SDK) tesseract-3.02.02-win32-lib-include-dirs(SDK)

    android-ndk-r16-beta1-windows-x86_64

    android-ndk-r16-beta1-windows-x86_64

    android-junit-report-dev

    1、 在应用tests目录文件下新建一个libs文件夹,将android-junit-report-dev.jar拷贝到该libs文件夹中 2、 在应用tests目录中的Android.mk文件加上如下配置: LOCAL_STATIC_JAVA_LIBRARIES += android-junit-report...

    Android代码-android-auto-scroll-view-pager

    Android Auto Scroll ViewPager ViewPager which can auto scrolling, cycling, decelerating. ViewPager which can be slided normal in parent ViewPager. 中文介绍见: Android自动滚动 轮播循环的ViewPager ...

    Android代码-android-sliding-layer-lib

    6Wunderkinder SlidingLayer for Android This repository hosts a library that provides an easy way to include an autonomous layer/view that slides from any side of your screen and which is fully gesture...

    android xml中include标签的使用

    关于xml布局中标签&lt;include /&gt;的使用的一个Demo

    Android中使用include标签和merge标签重复使用布局

    为了高效复用完整布局,你可以使用&lt;include&gt;和标签嵌入另一个布局到当前布局。所以当你通过写一个自定义视图创建独立UI组件,你可以放到一个布局文件里,这样更容易复用。 复用布局因为其允许你创建可复用的复杂布局...

    新版Android开发教程.rar

    的 Android SDK 提供了在 Android 平台上使用 JaVa 语言进行 Android 应用开发必须的工具和 API 接口。 特性 • 应用程序框架 支持组件的重用与替换 • Dalvik Dalvik Dalvik Dalvik 虚拟机 专为移动设备优化 • ...

    Android代码-android-edittext-validator

    Android Form EditText Android form edit text is an extension of EditText that brings data validation facilities to the edittext. Example App I built an example app that showcase some of the ...

    Android代码-mapbox-plugins-android

    Plugins are single-purpose libraries built on top of the Mapbox Maps SDK for Android that you can include in your apps like any other Android dependency Mapbox Plugins for Android Plugins are...

    tesseract-vs2013-include-lib-dll文件.rar

    tesseract-vs2013-include-lib-dll文件.rar,利用VS2013将tesseract3.0.2重新编译得到的库文件,include文件也包括其中,可以直接调用tesseract的API

    Android代码-android-api

    Square for Android API Square enables Android users to accept cash and card payments, and email receipts. The Square for Android API enables Android-based point-of-sale applications to accept payments...

    tesseract-3.02.02-win32-lib-include-dirs

    tesseract-3.02.02-win32-lib-include-dirs

    leptonica-1.68-win32-lib-include-dirs.rar

    leptonica-1.68-win32-lib-include-dirs 头文件和库文件

    Android代码-OpenWeatherMap-Android-Library

    OpenWeatherMap-Android-Library You need an API Key to use the OpenWeatherMap API. Head on over to their website if you don't already have one. Download Step 1. Add the JitPack repository to your root...

    前端开源库-markdown-it-include

    前端开源库-markdown-it-include它包括了markdown,markdown it插件,它添加了包括markdown片段文件的功能。

    tesseract-3.02.02-win32-lib-include-dirs.zip

    tesseract-3.02.02-win32-lib-include-dirs.zip win32 SDK

Global site tag (gtag.js) - Google Analytics