如何限制设计师的图片大小?

时间:2019-09-09 14:49:49

标签: c# xamarin xamarin.forms

美好的一天。 我在Xamarin中创建一个应用程序,我来自Android背景,在Android Studio中。由于我可以选择match_parent或wrap_content,因此无需手动设置组件大小。 Xamarin.Forms中有类似的东西吗?

我尝试使用“水平”和“垂直”选项,但似乎都不起作用。图像根本不合适。我想这是因为我试图将图像放入按钮中。

如下所示,尺寸不正确 incorrect size when in button 我试图使用按钮Padding甚至使用另一个StackLayout的Padding来设置Padding都没有成功。 除了使用图像组件并在其中为点击设置侦听器之外,还有其他解决方案吗? correct size when in img

2 个答案:

答案 0 :(得分:1)

我相信您要使用的控件是ImageButton。使用VerticalOptions和Horizo​​ntalOptions,您应该能够使控件正确居中。

<ImageButton Source="blue-play-button.png"
             VerticalOptions="CenterAndExpand"
             HorizontalOptions="CenterAndExpand" />

enter image description here

希望这会有所帮助。-

答案 1 :(得分:1)

就像pinedax所说的那样,您可以使用ImageButton,在设置 Horizo​​ntalOptions 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");
  }