티스토리 뷰

참고로, 미래 세대를 위해 Xamarin.FormsButton 클래스는 ContentLayout이제 값을 사용하여 속성을 지원 합니다 : 위쪽, 아래쪽, 왼쪽, 오른쪽 (이미지 위치). 따라서 원하는 출력을 위해 네이티브 버튼을 사용할 수 있습니다.

<Button   
 ContentLayout="Top"
  VerticalOptions="EndAndExpand"
   HorizontalOptions="CenterAndExpand"
    x:Name="search"
     Text="Search"   
      Image="Completed.png"
       IsEnabled="{Binding IsLoading}"
        Command="{Binding SearchCommand}"
         WidthRequest ="120"
          HeightRequest ="120"     
           TextColor="#FFFFFF"
           />
           

이 정보를 어디서 찾았는지 기억할 수 없지만 Button.csButtonRenderer.cs를 참조하십시오.

-------------------

나는 그 사실을 사용하는 것이 UIButton지원은 이미 지원 UIImage와 함께 Title당신은 단지 조정해야, 텍스트 ImageEdgeInsetsTitleEdgeInsets센터에 X / Y 정렬 및 설정을.

public class CenterImageButtonRenderer : ButtonRenderer
{
    public CenterImageButtonRenderer() { }
    
        protected override void OnElementChanged(ElementChangedEventArgs<Button> e)
            {
                    base.OnElementChanged(e);
                    
                            if (e.NewElement != null)
                                    {
                                                if (Control != null)
                                                            {
                                                                            Control.VerticalAlignment = UIControlContentVerticalAlignment.Center;
                                                                                            Control.HorizontalAlignment = UIControlContentHorizontalAlignment.Center;
                                                                                            
                                                                                                            #if DEBUG
                                                                                                                            // Set the border so we can see button bounds on a white background for testing alignment
                                                                                                                                            Control.Layer.CornerRadius = 15;
                                                                                                                                                            Control.Layer.BorderWidth = 5;
                                                                                                                                                                            Control.Layer.BorderColor = UIColor.Red.CGColor;
                                                                                                                                                                                            #endif
                                                                                                                                                                                            
                                                                                                                                                                                                            // The button image is not availabe due to lazy load, so we load "again", get the bounds and dispose of it... :-(
                                                                                                                                                                                                                            var uiImage = UIImage.FromFile(((Button)e.NewElement).Image.File);
                                                                                                                                                                                                                                            var lineHeight = Control.TitleLabel.Font.LineHeight;
                                                                                                                                                                                                                                                            Control.ImageEdgeInsets = new UIEdgeInsets(-lineHeight, uiImage.Size.Width + (uiImage.Size.Width / 2), 0, 0);
                                                                                                                                                                                                                                                                            Control.TitleEdgeInsets = new UIEdgeInsets(uiImage.Size.Height, -uiImage.Size.Width, 0, 0);
                                                                                                                                                                                                                                                                                            uiImage.Dispose();
                                                                                                                                                                                                                                                                                                        }
                                                                                                                                                                                                                                                                                                                }
                                                                                                                                                                                                                                                                                                                    }
                                                                                                                                                                                                                                                                                                                    }
                                                                                                                                                                                                                                                                                                                    

따라서 다음을 가정합니다.

var button = new CenterImageButton
{
    Text = "StackOverflow",
        Image = "so.png"
        };
        

결과는 다음과 같습니다.

여기에 이미지 설명 입력



출처
https://stackoverflow.com/questions/39917276
댓글
공지사항
Total
Today
Yesterday
«   2025/06   »
1 2 3 4 5 6 7
8 9 10 11 12 13 14
15 16 17 18 19 20 21
22 23 24 25 26 27 28
29 30