美文网首页鱼乐Android基础知识Android知识
Android布局优化之include标签详解

Android布局优化之include标签详解

作者: 我是吸血鬼 | 来源:发表于2016-03-19 15:10 被阅读2995次

一、include标签(布局重用)
1、说明:该标签的目的是解决重复定义布局的问题而诞生的,提高代码的复用
2、使用方法:

 <include 
        android:id="@+id/my_title_ly" 
        android:layout_width="match_parent" 
        android:layout_height="wrap_content" 
        layout="@layout/my_title_layout" /> 

3、举例说明:

    my_title_layout.xml
    <?xml version="1.0" encoding="utf-8"?> 
    <RelativeLayout                      
            xmlns:android="http://schemas.android.com/apk/res/android"        
            android:layout_width="match_parent"                                                                                                                                                           
            android:id="@+id/my_title_parent_id" 
            android:layout_height="wrap_content" >
       <ImageButton 
            android:id="@+id/back_btn"                                                                                                            
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" 
            android:src="@drawable/ic_launcher"   /> 

       <TextView android:id="@+id/title_tv"    
             android:layout_width="wrap_content" 
             android:layout_height="wrap_content"
             android:layout_centerVertical="true"
             android:layout_marginLeft="20dp"   
             android:layout_toRightOf="@+id/back_btn"
             android:gravity="center" 
             android:text="我的title" 
             android:textSize="18sp" />
      </RelativeLayout> 

include布局文件:

     <?xml version="1.0" encoding="utf-8"?> 
     <LinearLayout   
            xmlns:android="http://schemas.android.com/apk/res/android"           
            android:layout_width="match_parent"
            android:layout_height="match_parent" 
            android:orientation="vertical" > 
                                                                                                                            

        <include 
           android:id="@+id/my_title_ly" 
           android:layout_width="match_parent" 
           android:layout_height="wrap_content" 
           layout="@layout/my_title_layout" /> 

       </LinearLayout> 

4、注意事项:
子控件抛出空指针的情况
include标签若指定了ID属性,而你的layout也定义了ID,则你的layout的ID会被覆盖。如果findViewById()查找layout的Id来查找子控件,会出现这种情况。
解决:不用上边的方法。可以直接查找子控件的id。或者查找include标签的id来查找子控件。

相关文章

  • Android布局优化之include标签详解

    一、include标签(布局重用)1、说明:该标签的目的是解决重复定义布局的问题而诞生的,提高代码的复用。2、使用...

  • include与merge标签使用

    Android include与merge标签使用详解 简介include和merge标签的作用是实现布局文件的重...

  • Android app优化

    一、xml文件布局优化1.使用include 标签抽取多个布局文件共用的代码块,在需要的位置使用include标签...

  • Android 性能优化

    布局优化 include 标签 比如导航栏merge 标签 减少布局的层级viewstub 继承view 本身不...

  • Android布局优化之标签include,viewstub,m

    前言 布局优化对于每个项目总是必不可少,本文主要介绍使用抽象布局标签(include, viewstub, mer...

  • 笔记47 | Android性能优化之使用include标签重用

    地址 笔记47 | Android性能优化之使用include标签重用layouts(二) 目录 前言 创建可重用...

  • 安卓性能优化

    Android的性能优化方法 1. 布局优化 使用 标签、标签、 控件 复杂布局...

  • Android-性能优化

    应用体验-布局优化 使用include布局、merge标签、ViewStub视图可以使用HierarchyView...

  • android应用性能优化

    1. UI布局的优化 使用include,merge,ViewStub标签优化布局 尽量不存在冗余嵌套及过于复杂的...

  • Android 性能优化

    布局优化 include 标签。 merge 标签。 ViewStub 视图。 减少视图绘制:1.尽量避免在列表布...

网友评论

  • 李梦成:3Q,帮了大忙,我说怎么findId时include的布局找不到呢

本文标题:Android布局优化之include标签详解

本文链接:https://www.haomeiwen.com/subject/ybuhlttx.html