Example #1
0
        void rInflate(XmlPullParser parser, View parent, AttributeSet attrs, bool finishInflate, bool inheritContext)
        {
            int depth = parser.getDepth();
            int type;

            while (((type = parser.next()) != XmlPullParser.END_TAG || parser.getDepth() > depth) && type != XmlPullParser.END_DOCUMENT)
            {
                if (type != XmlPullParser.START_TAG)
                {
                    continue;
                }

                string name = parser.getName();

                if (TAG_REQUEST_FOCUS.Equals(name))
                {
                    parseRequestFocus(parser, parent);
                }
                else if (TAG_TAG.Equals(name))
                {
                    parseViewTag(parser, parent, attrs);
                }

                if (TAG_INCLUDE.Equals(name))
                {
                    if (parser.getDepth() == 0)
                    {
                        throw new Exception("<include /> cannot be the root element");
                    }

                    parseInclude(parser, parent, attrs, inheritContext);
                }
                else if (TAG_MERGE.Equals(name))
                {
                    throw new Exception("<merge /> must be the root element");
                }

                else
                {
                    View      view                 = createViewFromTag(parent, name, attrs, inheritContext);
                    ViewGroup viewGroup            = (ViewGroup)parent;
                    ViewGroup.LayoutParams lparams = viewGroup.generateLayoutParams(attrs);
                    rInflate(parser, view, attrs, true, true);
                    viewGroup.addView(view, lparams);
                }
            }

            if (finishInflate)
            {
                parent.onFinishInflate();
            }
        }
Example #2
0
        public void setLayoutParams(ViewGroup.LayoutParams lparams)
        {
            layparams = lparams;

            switch (lparams.width)
            {
            case ViewGroup.LayoutParams.MATCH_PARENT:
                WinUI.HorizontalAlignment = Windows.UI.Xaml.HorizontalAlignment.Stretch;
                break;

            case ViewGroup.LayoutParams.WRAP_CONTENT:
                WinUI.Width = double.NaN;
                break;

            case 0:
                break;

            default:
                WinUI.Width = lparams.width;
                break;
            }

            switch (lparams.height)
            {
            case ViewGroup.LayoutParams.MATCH_PARENT:
                WinUI.VerticalAlignment = Windows.UI.Xaml.VerticalAlignment.Stretch;
                break;

            case ViewGroup.LayoutParams.WRAP_CONTENT:
                WinUI.Height = double.NaN;
                break;

            case 0:
                break;

            default:
                WinUI.Height = lparams.height;
                break;
            }

            if (lparams.GetType().Equals(typeof(ViewGroup.MarginLayoutParams)))
            {
                ViewGroup.MarginLayoutParams lparams2 = (ViewGroup.MarginLayoutParams)lparams;
                WinUI.Margin = new Windows.UI.Xaml.Thickness(lparams2.leftMargin, lparams2.topMargin, lparams2.rightMargin, lparams2.bottomMargin);
            }
        }
Example #3
0
 public abstract void setContentView(View view, ViewGroup.LayoutParams _params);
Example #4
0
        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);
        }
Example #5
0
        private void parseInclude(XmlPullParser parser, View parent, AttributeSet attrs, bool inheritContext)
        {
            int type;

            //if (parent.GetType().Equals(typeof(ViewGroup)))
            if (parent is ViewGroup)
            {
                int layout = attrs.getAttributeResourceValue(null, "layout", 0); //This should return resource value, not name
                if (layout == 0)
                {
                    string value = attrs.getAttributeValue(null, "layout");
                    if (value == null)
                    {
                        throw new Exception("You must specifiy a layout in the" + " include tag: <include layout=\"@layout/layoutID\" />");
                    }
                    else
                    {
                        throw new Exception("You must specifiy a valid layout " + "reference. The layout ID " + value + " is not valid.");
                    }
                }

                else
                {
                    XmlResourceParser childParser = getContext().getResources().getLayout(layout);

                    try
                    {
                        AttributeSet childAttrs = Xml.asAttributeSet(childParser, mContext);

                        while ((type = childParser.next()) != XmlPullParser.START_TAG && type != XmlPullParser.END_DOCUMENT)
                        {
                            // Empty.
                        }

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

                        string childName = childParser.getName();

                        if (TAG_MERGE.Equals(childName))
                        {
                            // Inflate all children.
                            rInflate(childParser, parent, childAttrs, false, inheritContext);
                        }
                        else
                        {
                            View      view  = createViewFromTag(parent, childName, childAttrs, inheritContext);
                            ViewGroup group = (ViewGroup)parent;

                            // We try to load the layout params set in the <include /> tag. If
                            // they don't exist, we will rely on the layout params set in the
                            // included XML file.
                            // During a layoutparams generation, a runtime exception is thrown
                            // if either layout_width or layout_height is missing. We catch
                            // this exception and set localParams accordingly: true means we
                            // successfully loaded layout params from the <include /> tag,
                            // false means we need to rely on the included layout params.
                            ViewGroup.LayoutParams lparams = null;
                            try
                            {
                                lparams = group.generateLayoutParams(attrs);
                            }
                            catch
                            {
                                lparams = group.generateLayoutParams(childAttrs);
                            }
                            finally
                            {
                                if (lparams != null)
                                {
                                    view.setLayoutParams(lparams);
                                }
                            }

                            // Inflate all children.
                            rInflate(childParser, view, childAttrs, true, true);

                            /*
                             * // Attempt to override the included layout's android:id with the
                             * // one set on the <include /> tag itself.
                             * TypedArray a = mContext.obtainStyledAttributes(attrs, com.android.inner.R.styleable.View, 0, 0);
                             * int id = a.getResourceId(com.android.inner.R.styleable.View_id, View.NO_ID);
                             * // While we're at it, let's try to override android:visibility.
                             * int visibility = a.getInt(com.android.inner.R.styleable.View_visibility, -1);
                             * a.recycle();
                             *
                             * if (id != View.NO_ID)
                             * {
                             *  view.setId(id);
                             * }
                             *
                             * switch (visibility)
                             * {
                             *  case 0:
                             *      view.setVisibility(View.VISIBLE);
                             *      break;
                             *  case 1:
                             *      view.setVisibility(View.INVISIBLE);
                             *      break;
                             *  case 2:
                             *      view.setVisibility(View.GONE);
                             *      break;
                             * }
                             */

                            group.addView(view);
                        }
                    }

                    finally { }
                }
            }
            else
            {
                throw new Exception("<include /> can only be used inside of a ViewGroup");
            }

            int currentDepth = parser.getDepth();

            while (((type = parser.next()) != XmlPullParser.END_TAG || parser.getDepth() > currentDepth) && type != XmlPullParser.END_DOCUMENT)
            {
                // Empty
            }
        }