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/AppCompat | |
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/AppCompat')
-rw-r--r-- | Xamarin.Forms.Platform.Android/AppCompat/ButtonRenderer.cs | 49 |
1 files changed, 42 insertions, 7 deletions
diff --git a/Xamarin.Forms.Platform.Android/AppCompat/ButtonRenderer.cs b/Xamarin.Forms.Platform.Android/AppCompat/ButtonRenderer.cs index c7df08e2..7b7503e0 100644 --- a/Xamarin.Forms.Platform.Android/AppCompat/ButtonRenderer.cs +++ b/Xamarin.Forms.Platform.Android/AppCompat/ButtonRenderer.cs @@ -158,16 +158,51 @@ namespace Xamarin.Forms.Platform.Android.AppCompat void UpdateBitmap() { - FileImageSource elementImage = Element.Image; - string imageFile = elementImage?.File; - if (elementImage != null && !string.IsNullOrEmpty(imageFile)) + var elementImage = Element.Image; + var imageFile = elementImage?.File; + + if (elementImage == null || string.IsNullOrEmpty(imageFile)) + { + Control.SetCompoundDrawablesWithIntrinsicBounds(null, null, null, null); + return; + } + + var image = Context.Resources.GetDrawable(imageFile); + + if (string.IsNullOrEmpty(Element.Text)) { - Drawable image = Context.Resources.GetDrawable(imageFile); - Control.SetCompoundDrawablesWithIntrinsicBounds(image, null, null, null); + // 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; } - else - Control.SetCompoundDrawablesWithIntrinsicBounds(null, null, null, null); + + 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 UpdateEnabled() |