コード例 #1
0
        public void OnLayout(bool changed, int left, int top, int right, int bottom)
        {
            if (_disposed || _renderer == null || _element == null)
            {
                return;
            }

            AButton view = View;

            if (view == null)
            {
                return;
            }

            Drawable drawable = null;

            Drawable[] drawables = TextViewCompat.GetCompoundDrawablesRelative(view);
            if (drawables != null)
            {
                foreach (var compoundDrawable in drawables)
                {
                    if (compoundDrawable != null)
                    {
                        drawable = compoundDrawable;
                        break;
                    }
                }
            }

            if (drawable != null)
            {
                int iconWidth = drawable.IntrinsicWidth;
                drawable.CopyBounds(_drawableBounds);

                // Center the drawable in the button if there is no text.
                // We do not need to undo this as when we get some text, the drawable recreated
                if (string.IsNullOrEmpty(_element.Text))
                {
                    var newLeft = (right - left - iconWidth) / 2 - view.PaddingLeft;

                    _drawableBounds.Set(newLeft, _drawableBounds.Top, newLeft + iconWidth, _drawableBounds.Bottom);
                    drawable.Bounds = _drawableBounds;
                }
                else
                {
                    if (_alignIconWithText && _element.ContentLayout.IsHorizontal())
                    {
                        var buttonText = view.TextFormatted;

                        // if text is transformed, add that transformation to to ensure correct calculation of icon padding
                        if (view.TransformationMethod != null)
                        {
                            buttonText = view.TransformationMethod.GetTransformationFormatted(buttonText, view);
                        }

                        var measuredTextWidth = view.Paint.MeasureText(buttonText, 0, buttonText.Length());
                        var textWidth         = Math.Min((int)measuredTextWidth, view.Layout.Width);
                        var contentsWidth     = ViewCompat.GetPaddingStart(view) + iconWidth + view.CompoundDrawablePadding + textWidth + ViewCompat.GetPaddingEnd(view);

                        var newLeft = (view.MeasuredWidth - contentsWidth) / 2;
                        if (_element.ContentLayout.Position == Button.ButtonContentLayout.ImagePosition.Right)
                        {
                            newLeft = -newLeft;
                        }
                        if (ViewCompat.GetLayoutDirection(view) == ViewCompat.LayoutDirectionRtl)
                        {
                            newLeft = -newLeft;
                        }

                        _drawableBounds.Set(newLeft, _drawableBounds.Top, newLeft + iconWidth, _drawableBounds.Bottom);
                        drawable.Bounds = _drawableBounds;
                    }
                }
            }

            _hasLayoutOccurred = true;
        }