Exemple #1
0
        /**
         * Create from inside an XML document. Called on a parser positioned at a
         * tag in an XML document, tries to create a ColorStateList from that tag.
         *
         * @throws XmlPullParserException if the current tag is not <selector>
         * @return A color state list for the current tag.
         */
        private static ColorStateList createFromXmlInner(Resources r, XmlPullParser parser, AttributeSet attrs)
        {
            ColorStateList colorStateList;
            string         name = parser.getName();

            if (name.Equals("selector"))
            {
                colorStateList = new ColorStateList();
            }
            else
            {
                throw new Exception(parser.getPositionDescription() + ": invalid drawable tag " + name);
            }

            //colorStateList.inflate(r, parser, attrs);
            return(colorStateList);
        }
        public virtual View inflate(XmlPullParser parser, ViewGroup root, bool attachToRoot)
        {
            //Trace.traceBegin(Trace.TRACE_TAG_VIEW, "inflate");

            AttributeSet attrs       = Xml.asAttributeSet(parser, mContext);
            Context      lastContext = (Context)mConstructorArgs[0];

            mConstructorArgs[0] = mContext;
            View result = root;

            try
            {
                // Look for the root node.
                int type;
                while ((type = parser.next()) != XmlPullParser.START_TAG && type != XmlPullParser.END_DOCUMENT)
                {
                    // Empty
                    //"WHAT A MISTAKE!" - DJT; but this is how Android does it so we'll do it here :(
                }

                if (type != XmlPullParser.START_TAG)
                {
                    throw new Exception(parser.getPositionDescription() + ": No start tag found!");
                }

                string name = parser.getName();

                if (DEBUG)
                {
                    Debug.WriteLine("**************************");
                    Debug.WriteLine("Creating root view: " + name);
                    Debug.WriteLine("**************************");
                }

                if (TAG_MERGE.Equals(name))
                {
                    if (root == null || !attachToRoot)
                    {
                        throw new Exception("<merge /> can be used only with a valid " + "ViewGroup root and attachToRoot=true");
                    }

                    rInflate(parser, root, attrs, false, false);
                }

                else
                {
                    // Temp is the root view that was found in the xml
                    View temp = createViewFromTag(root, name, attrs, false);

                    ViewGroup.LayoutParams lparams = null;

                    if (root != null)
                    {
                        if (DEBUG)
                        {
                            Debug.WriteLine("Creating params from root: " + root);
                        }

                        // Create layout params that match root, if supplied
                        lparams = root.generateLayoutParams(attrs);

                        if (!attachToRoot)
                        {
                            // Set the layout params for temp if we are not
                            // attaching. (If we are, we use addView, below)
                            temp.setLayoutParams(lparams);
                        }
                    }

                    if (DEBUG)
                    {
                        Debug.WriteLine("-----> start inflating children");
                    }

                    // Inflate all children under temp
                    rInflate(parser, temp, attrs, true, true);

                    if (DEBUG)
                    {
                        Debug.WriteLine("-----> done inflating children");
                    }

                    // We are supposed to attach all the views we found (int temp)
                    // to root. Do that now.
                    if (root != null && attachToRoot)
                    {
                        root.addView(temp, lparams);
                    }

                    // Decide whether to return the root that was passed in or the
                    // top view found in xml.
                    if (root == null || !attachToRoot)
                    {
                        result = temp;
                    }
                }
            }
            catch
            {
                throw;
            }

            finally
            {
                // Don't retain static reference on context.
                mConstructorArgs[0] = lastContext;
                mConstructorArgs[1] = null;
            }

            //Trace.traceEnd(Trace.TRACE_TAG_VIEW);

            return(result);
        }