Android: Custom View和include标签的区别

简介:

Custom View, 使用的时候是这样的:

    <com.example.home.alltest.view.MyCustomView
        android:id="@+id/customView"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">
    </com.example.home.alltest.view.MyCustomView>

使用的时候是这样的:

_customView = (MyCustomView)findViewById(R.id.customView1);
_customView.setText("What's", "UP?");

但是include是一个表情,在布局中使用的时候是这样的:

<include layout="@layout/view_my_custom"
        android:id="@+id/customView1" />

这个表情将会被layout属性制定的布局文件替换掉,所以这里的include只是view_my_custom布局文件的RelativeLayout占位符。

在代码中使用的时候是这样的:

RelativeLayout _relativeLayout = (RelativeLayout)findViewById(R.id.customView1);
TextView textWorld = (TextView)_relativeLayout.findViewById(R.id.txtWorld);

区别就是,第一种方法可以直接findViewById之后当做你的自定义类来使用。而include标签只是占位符,在编译的时候这里就变成了你的自定义空间的那一堆各种Layout了。所以在使用的时候只能在脑中把被替换掉的layout文件再补回来。

欢迎加群互相学习,共同进步。QQ群:iOS: 58099570 | Android: 330987132 | Go:217696290 | Python:336880185 | 做人要厚道,转载请注明出处!http://www.cnblogs.com/sunshine-anycall/p/4819475.html
相关文章
|
消息中间件 存储 弹性计算
RocketMQ 5.0: 存储计算分离新思路
在阿里云上,RocketMQ 的商业化产品也以弹性云服务的形式为全球数万个用户提供企业级的消息解决方案,被广泛应用于互联网、大数据、移动互联网、物联网等领域的业务场景,成为了业务开发的首选消息中间件。
2009 124
RocketMQ 5.0: 存储计算分离新思路
|
Linux
linux下ffmpeg安装
<p style="border: 0px; font-family: Muli, sans-serif; font-size: 15px; margin-top: 0px; margin-bottom: 1.5em; outline: 0px; padding-top: 0px; padding-bottom: 0px; vertical-align: baseline; word-wrap
10400 57
【Liunx系统编程】命令模式3--1
【Liunx系统编程】命令模式3--1
|
文字识别 Java Android开发
autojs之ocr-腾讯
使用情景 安卓手机调用腾讯ocr识别文字
770 0
autojs之ocr-腾讯
|
安全 Android开发
【Android 逆向】整体加固脱壳 ( DexClassLoader 加载 dex 流程分析 | RawDexFile.cpp 分析 | dvmRawDexFileOpen函数读取 DEX 文件 )
【Android 逆向】整体加固脱壳 ( DexClassLoader 加载 dex 流程分析 | RawDexFile.cpp 分析 | dvmRawDexFileOpen函数读取 DEX 文件 )
417 0
|
Linux Shell C语言
EDKII Build Process:EDKII项目源码的配置、编译流程[三]
<p style="margin-top: 0px; margin-bottom: 0px; padding-top: 0px; padding-bottom: 0px; font-family: Tahoma; line-height: 24px; font-size: 12.381px;"><span style="margin: 0px; padding: 0px; list-style
5383 0
|
Perl 流计算 内存技术
AXI总线简介
<p style="margin-top: 14px; margin-bottom: 14px; padding-top: 0px; padding-bottom: 0px; color: rgb(46, 46, 46); font-family: 微软雅黑, 'Microsoft Yahei'; font-size: 14px; line-height: 28px; border: 0px;
5543 0
|
资源调度 算法 Java
图像处理------Canny边缘检测
<p style="color: rgb(51, 51, 51); font-family: Arial; font-size: 14px; line-height: 26px;"><span style="font-size: 18px;"><strong>一:历史</strong></span></p> <p style="color: rgb(51, 51, 51); font-fami
2101 0
|
算法 Java 存储
图像分析------连通组件标记算法
<p style="color: rgb(51, 51, 51); font-family: Arial; font-size: 14px; line-height: 26px;">连接组件标记算法(connected component labeling algorithm)是图像分析中最常用的算法之一,</p> <p style="color: rgb(51, 51, 51); font-
2542 0