main.xml中的Android自定义xml标记

问题描述

| 我正在修改Android终端仿真器程序的源代码,该程序在main.xml的线性布局中包含以下令人困惑的xml代码
<com.vtrandal.bluesentry.EmulatorView
    android:id=\"@+id/emulatorView\"
    android:layout_width=\"wrap_content\"
    android:layout_height=\"wrap_content\"
    android:layout_alignParentLeft=\"true\"
    />
同样的xml代码也出现在另一个xml文件中,该文件也与main.xml一起在/ res / layout中。我可以看到它似乎始于使用我的包名称创建的自定义标签。但为什么?这怎么可能?为什么有人想要这样的东西?     

解决方法

扩展视图以创建自定义组件时,需要一种方法将此对象添加到xml布局中。您可以通过使用完整的类名(包括程序包名)来实现。 Nicholas先前发布的开发人员指南链接对此进行了解释。 该标记内使用的属性可以是您要扩展的View所使用的属性,也可以是在新架构中定义的自定义属性。 您可以在此处遵循一个不错的教程:http://www.anddev.org/creating_custom_views_-_the_togglebutton-t310.html     ,这是在Android中创建自定义组件和视图的语法。     ,不知道到底是什么令人困惑。我假设它的ѭ1部分。这是膨胀xml文件时将实例化的类的名称。 如果您习惯这样的事情:
<TextView
   android:id=\"@+id/emulatorView\"
   android:layout_width=\"wrap_content\"
   android:layout_height=\"wrap_content\"
   android:layout_alignParentLeft=\"true\"
/>
然后您应该知道这是因为
TextView
位于
android.widget
中,并且您应该这样写:
<android.widget.TextView
   android:id=\"@+id/emulatorView\"
   android:layout_width=\"wrap_content\"
   android:layout_height=\"wrap_content\"
   android:layout_alignParentLeft=\"true\"
/>
但是,由于Inflater知道几个特殊软件包(
android.widget
android.view
android.webkit
),因此您可以在xml中跳过它们。