美好的一天。 我在Xamarin中创建一个应用程序,我来自Android背景,在Android Studio中。由于我可以选择match_parent或wrap_content,因此无需手动设置组件大小。 Xamarin.Forms中有类似的东西吗?
我尝试使用“水平”和“垂直”选项,但似乎都不起作用。图像根本不合适。我想这是因为我试图将图像放入按钮中。
如下所示,尺寸不正确
我试图使用按钮Padding甚至使用另一个StackLayout的Padding来设置Padding都没有成功。
除了使用图像组件并在其中为点击设置侦听器之外,还有其他解决方案吗?
答案 0 :(得分:1)
我相信您要使用的控件是ImageButton
。使用VerticalOptions和HorizontalOptions,您应该能够使控件正确居中。
<ImageButton Source="blue-play-button.png"
VerticalOptions="CenterAndExpand"
HorizontalOptions="CenterAndExpand" />
希望这会有所帮助。-
答案 1 :(得分:1)
就像pinedax所说的那样,您可以使用ImageButton
,在设置 HorizontalOptions 和 VerticalOptions 之后,可以通过 HeightRequest 限制其大小>和 WidthRequest ,纵横比属性的大小在要显示的图像范围内(无论是拉伸,裁切还是信箱),也可以在其中设置点击监听器像按钮:
<ImageButton Source="play_button.png" WidthRequest="300" HeightRequest="300" Aspect="Fill" HorizontalOptions="Center" VerticalOptions="Center" Clicked="ImageButton_Clicked"></ImageButton>
在后面的代码中:
private void ImageButton_Clicked(object sender, EventArgs e)
{
Console.WriteLine("clicked");
}