Connection between View in the xml file and extened View class

浪子不回头ぞ 提交于 2019-12-06 09:45:13

问题


Is there a way to link inherit class to xml file.

I am trying to connect extended class to widget in the xml file.

Is it possible ?

Thanks in advance.


回答1:


You must have noticed that all the nodes that we specify in the layout XMLs are actually either View classes(for e.g: TextView, EditView) or view containers/layout managers(e.g: LinearLayout, RelativeLayout etc.). Android allows you to create custom views and containers by extending the View class and one of the layout managers, respectively. You can then choose to inflate such views directly from code or specify them as nodes in your layout XMLs.

For instance, assuming you create a View class such as:

public class com.views.MyView extends View{}

then you can include this class directly in you layout XML by saying:

<LinearLayout ..>
    <com.views.MyView .. />
</LinearLayout>

Note that when you specify your View class directly in XML there are a few important subtleties to consider such as: When inflating the custom view, the framework will call different constructor of the view. The arguments would be a context object and AttributeSet(containing attributes you set in the XML).
For more details refer this



来源:https://stackoverflow.com/questions/1957252/connection-between-view-in-the-xml-file-and-extened-view-class

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!