开发者

Android custom xml tag in main.xml

I'm modifying the source code of a terminal emulator program for Android that contains the following confusing xml code in the linear layout of main.xml:

<com.vtrandal.bluesentry.EmulatorView
    android:id="@+id/emulatorView"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentLeft="true"
    />

This same xml code also appears in another xml file also in /res/layout along with main.xml. I can see that it appears to begin with a cus开发者_如何学Gotom tag created using my package name. But why? How is this possible and why would someone want something like this?


When you extend a View to create a custom component, you need a way to add this objects to the xml layout. You do that by using the full class name, including the package name. The link to the dev guide the Nicholas posted previously explains it.

The attributes used inside that tag can be either attributes used for the View you are extending, or custom attributes defined in a new schema.

You can follow a nice tutorial about this here: http://www.anddev.org/creating_custom_views_-_the_togglebutton-t310.html


This is the syntax for creating custom components and views in Android.


Not sure what exactly is confusing. I'll assume its com.vtrandal.bluesentry.EmulatorView part. This is name of the class that will be instantiated when inflating xml file.

If you got used to something like this:

<TextView
   android:id="@+id/emulatorView"
   android:layout_width="wrap_content"
   android:layout_height="wrap_content"
   android:layout_alignParentLeft="true"
/>

Then you should know that this is because TextView is located in android.widget, and you should've wrote this:

<android.widget.TextView
   android:id="@+id/emulatorView"
   android:layout_width="wrap_content"
   android:layout_height="wrap_content"
   android:layout_alignParentLeft="true"
/>

But because inflater knows about several special packages (android.widget, android.view, android.webkit) you can skip them in xml.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜