eclipse 中也有自訂view的XML可使用,但是套用下去卻會產生,Custom view XXXView is not using the 2- or 3-argument View constructors; XML attributes will not work這個錯誤訊息.

 

在此說明完整的自訂view的方法.自己當一個紀錄.

1.在XML的palette中的Advanced找到小v(view) 把它拉進去你要的layout裡面.

20140422001  

2.出現視窗指定程式class.

3.若指定的程式class建構不全則會出現錯誤訊息Custom view XXXView is not using the 2- or 3-argument View constructors; XML attributes will not work

20140422002  

4.在自訂view class中加入

(Context context, AttributeSet attrs) {
super(context, attrs);

這樣問題就可解決了

開啟宣告 android.util.AttributeSet

 

A collection of attributes, as found associated with a tag in an XML document. Often you will not want to use this interface directly, instead passing it to Resources.Theme.obtainStyledAttributes() which will take care of parsing the attributes for you. In particular, the Resources API will convert resource references (attribute values such as "@string/my_label" in the original XML) to the desired type for you; if you use AttributeSet directly then you will need to manually check for resource references (with getAttributeResourceValue(int, int)) and do the resource lookup yourself if needed. Direct use of AttributeSet also prevents the application of themes and styles when retrieving attribute values.

This interface provides an efficient mechanism for retrieving data from compiled XML files, which can be retrieved for a particular XmlPullParser through Xml.asAttributeSet(). Normally this will return an implementation of the interface that works on top of a generic XmlPullParser, however it is more useful in conjunction with compiled XML resources:

 XmlPullParser parser = resources.getXml(myResouce);
 AttributeSet attributes = Xml.asAttributeSet(parser);

The implementation returned here, unlike using the implementation on top of a generic XmlPullParser, is highly optimized by retrieving pre-computed information that was generated by aapt when compiling your resources. For example, the getAttributeFloatValue(int, float) method returns a floating point number previous stored in the compiled resource instead of parsing at runtime the string originally in the XML file.

This interface also provides additional information contained in the compiled XML resource that is not available in a normal XML file, such as getAttributeNameResource(int) which returns the resource identifier associated with a particular XML attribute name.

簡單的說就是讓你的calss 有parser XML的能力.

arrow
arrow

    台灣蝸牛 發表在 痞客邦 留言(0) 人氣()