是否可以在Xamarin.Forms中使用ImageSpan?

时间:2014-08-24 06:31:59

标签: xamarin.android xamarin.forms

我正在尝试调整Xamarin.Forms.Labs中的ImageButtonRenderer,以使用图像&渲染ImageButtons。文本居中(作为块)在图像按钮上,而不是使图像与按钮的左/右边缘对齐,这是渲染器的默认行为。

我遇到一个StackOverflow article声明可以在Android中使用ImageSpan实现这种布局 - 但是,当我将代码翻译成Xamarin.Forms C#时,会显示文本,但不显示图像。任何人都可以看到这个代码明显错误吗?...

来源是ImageButton控件的ImageSource值

var handler = new FileImageSourceHandler();

var bitmap = await handler.LoadImageAsync(source, this.Context);

if (bitmap != null)
{
    var bitmapWidth = this.ImageButton.ImageWidthRequest.Equals(0) ? bitmap.Width : (int)(this.ImageButton.ImageWidthRequest);
    var bitmapHeight = this.ImageButton.ImageHeightRequest.Equals(0) ? bitmap.Height : (int)(this.ImageButton.ImageHeightRequest);

    Drawable drawable = new BitmapDrawable(Resources, bitmap);
    var scaledDrawable = new ScaleDrawable(drawable, 0, bitmapWidth, bitmapHeight).Drawable;
    scaledDrawable.SetBounds(0, 0, bitmapWidth, bitmapHeight);

    var scaledBitmap = Bitmap.CreateScaledBitmap(bitmap, bitmapWidth, bitmapHeight, true);

    var leftPadding = (int)this.ImageButton.ButtonPadding.Left;
    var rightPadding = (int)this.ImageButton.ButtonPadding.Right;
    var topPadding = (int)this.ImageButton.ButtonPadding.Top;
    var bottomPadding = (int)this.ImageButton.ButtonPadding.Bottom;

    Control.SetPadding(leftPadding, topPadding, rightPadding, bottomPadding);
    Control.Gravity = GravityFlags.CenterHorizontal | GravityFlags.CenterVertical;

    switch (this.ImageButton.Orientation)
    {
        case ImageOrientation.ImageToRight:
            var rightButtonLabel = new SpannableString(string.Format("{0} ", this.Control.Text));
            //rightButtonLabel.SetSpan(new ImageSpan(scaledBitmap), rightButtonLabel.Length() - 1, rightButtonLabel.Length(), SpanTypes.InclusiveInclusive);
            //rightButtonLabel.SetSpan(new ImageSpan(scaledDrawable, SpanAlign.Bottom), rightButtonLabel.Length() - 1, rightButtonLabel.Length(), SpanTypes.InclusiveInclusive);
            //rightButtonLabel.SetSpan(new ImageSpan(scaledDrawable), rightButtonLabel.Length() - 1, rightButtonLabel.Length(), SpanTypes.InclusiveInclusive);
            rightButtonLabel.SetSpan(new ImageSpan(Forms.Context, scaledBitmap, SpanAlign.Bottom), rightButtonLabel.Length() - 1, rightButtonLabel.Length(), SpanTypes.InclusiveInclusive);
            Control.SetText(rightButtonLabel, Android.Widget.TextView.BufferType.Spannable);
            break;

        case ImageOrientation.ImageToLeft:
        default:
            var leftButtonLabel = new SpannableString(string.Format(" {0}", this.Control.Text));
            //leftButtonLabel.SetSpan(new ImageSpan(scaledBitmap), 0, 1, SpanTypes.InclusiveInclusive);
            //leftButtonLabel.SetSpan(new ImageSpan(scaledDrawable, SpanAlign.Bottom), 0, 1, SpanTypes.InclusiveInclusive);
            //leftButtonLabel.SetSpan(new ImageSpan(scaledDrawable), 0, 1, SpanTypes.ExclusiveExclusive);
            leftButtonLabel.SetSpan(new ImageSpan(Forms.Context, scaledBitmap, SpanAlign.Bottom), 0, 1, SpanTypes.InclusiveInclusive);
            Control.SetText(leftButtonLabel, Android.Widget.TextView.BufferType.Spannable);
            break;
    }
}

文本中心很好,但没有图像。我使用Forms.Context尝试了一些与Drawable,Bitmaps不同的ImageSpan重载,但没有一个能够工作。

在Xamarin.Forms中使用ImageSpan有一些限制吗?

1 个答案:

答案 0 :(得分:-1)

是的,这是一年前的问题。无论如何,我想this帖可以帮助解决这个问题。

只需将图片加载到某些(不可见)视图,然后就会显示ImageSpan张图片。