diff options
author | E.Z. Hart <hartez@gmail.com> | 2016-03-22 17:16:53 -0600 |
---|---|---|
committer | E.Z. Hart <hartez@gmail.com> | 2016-03-30 16:22:35 -0600 |
commit | 17e2a4b94d5105b53c5875d9416d043f14313305 (patch) | |
tree | 6f03e5d6f562f230ed79d2e820327fe0bddc5069 /Xamarin.Forms.Platform.Android/Renderers | |
parent | b3a64fadb3951d4e5e65b5f000a5f930772c01ed (diff) | |
download | xamarin-forms-17e2a4b94d5105b53c5875d9416d043f14313305.tar.gz xamarin-forms-17e2a4b94d5105b53c5875d9416d043f14313305.tar.bz2 xamarin-forms-17e2a4b94d5105b53c5875d9416d043f14313305.zip |
Add options for specifying layout of button text/image content
Also make the layout and layout defaults consistent across platforms
Diffstat (limited to 'Xamarin.Forms.Platform.Android/Renderers')
-rw-r--r-- | Xamarin.Forms.Platform.Android/Renderers/ButtonRenderer.cs | 52 |
1 files changed, 44 insertions, 8 deletions
diff --git a/Xamarin.Forms.Platform.Android/Renderers/ButtonRenderer.cs b/Xamarin.Forms.Platform.Android/Renderers/ButtonRenderer.cs index eb9b884f..0d93207a 100644 --- a/Xamarin.Forms.Platform.Android/Renderers/ButtonRenderer.cs +++ b/Xamarin.Forms.Platform.Android/Renderers/ButtonRenderer.cs @@ -138,17 +138,53 @@ namespace Xamarin.Forms.Platform.Android UpdateDrawable(); } - async void UpdateBitmap() + void UpdateBitmap() { - if (Element.Image != null && !string.IsNullOrEmpty(Element.Image.File)) + var elementImage = Element.Image; + var imageFile = elementImage?.File; + + if (elementImage == null || string.IsNullOrEmpty(imageFile)) { - Drawable image = Context.Resources.GetDrawable(Element.Image.File); - Control.SetCompoundDrawablesWithIntrinsicBounds(image, null, null, null); - if (image != null) - image.Dispose(); - } - else Control.SetCompoundDrawablesWithIntrinsicBounds(null, null, null, null); + return; + } + + var image = Context.Resources.GetDrawable(imageFile); + + if (string.IsNullOrEmpty(Element.Text)) + { + // No text, so no need for relative position; just center the image + // There's no option for just plain-old centering, so we'll use Top + // (which handles the horizontal centering) and some tricksy padding + // to handle the vertical centering + Control.SetCompoundDrawablesWithIntrinsicBounds(null, image, null, null); + Control.SetPadding(0, Control.PaddingTop, 0, -Control.PaddingTop); + image?.Dispose(); + return; + } + + var layout = Element.ContentLayout; + + Control.CompoundDrawablePadding = (int)layout.Spacing; + + switch (layout.Position) + { + case Button.ButtonContentLayout.ImagePosition.Top: + Control.SetCompoundDrawablesWithIntrinsicBounds(null, image, null, null); + break; + case Button.ButtonContentLayout.ImagePosition.Bottom: + Control.SetCompoundDrawablesWithIntrinsicBounds(null, null, null, image); + break; + case Button.ButtonContentLayout.ImagePosition.Right: + Control.SetCompoundDrawablesWithIntrinsicBounds(null, null, image, null); + break; + default: + // Defaults to image on the left + Control.SetCompoundDrawablesWithIntrinsicBounds(image, null, null, null); + break; + } + + image?.Dispose(); } void UpdateDrawable() |