如何在 Xamarin.Forms 中的按钮上设置图像高度



>点击这里查看所需的设计

我想要一个像上面(所需的设计(一样的页脚结构在我的应用程序中,它有两个宽度为 50% 的按钮,上面有 fb 和谷歌图像。但是我无法设置图像的高度。请帮帮我

这是我尝试过的代码

<StackLayout>
<StackLayout Orientation="Horizontal" VerticalOptions="Start">
<!-- top controls -->
</StackLayout>
<StackLayout VerticalOptions="CenterAndExpand">
<!-- middle controls -->
</StackLayout>
<StackLayout Orientation="Horizontal" VerticalOptions="End">
<StackLayout Orientation="Horizontal"  HorizontalOptions="FillAndExpand">
<StackLayout Orientation="Vertical"  HorizontalOptions="FillAndExpand">
<Button Text="Has Image" Image="drawable/fb_btn.png"/>
</StackLayout>
<StackLayout Orientation="Vertical"  HorizontalOptions="FillAndExpand">
<Button Text="Sign Up"/>
</StackLayout>
</StackLayout>
</StackLayout>
</StackLayout>

解决方法

1.您可以通过点击手势使用图像控制:

XAML

<Image x:Name="myImage" Source="myPicture.png" HeightRequest="150" WidthRequest="60" BackgroundColor="Red" HorizontalOptions="Center"/>

XAML.CS

TapGestureRecognizer tapEvent = new TapGestureRecognizer();
tapEvent.Tapped += clickedEvent;
myImage.GestureRecognizers.Add(tapEvent);

2. 参考 https://github.com/XLabs/Xamarin-Forms-Labs/wiki/ImageButton

您可以在代码中添加HeightRequest,如下所示:

XAML

<StackLayout Orientation="Vertical"  HorizontalOptions="FillAndExpand">
<Button Name="imageBtn" Text="Has Image" Image="drawable/fb_btn.png" 
HeightRequest="56"/>
</StackLayout>

更多信息:

1,您的代码包含堆栈布局,它也可以修改结果。

2,如果您想更好地控制图像大小和位置,可以为其添加点击手势识别器。

。.CS

TapGestureRecognizer tapEvent = new TapGestureRecognizer();
tapEvent.Tapped += Button_Clicked;
imageBtn.GestureRecognizers.Add(tapEvent);

最新更新