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

时间:2018-10-25 13:23:45

标签: xamarin xamarin.forms

Click here to see the desired design

我想在我的应用程序中使用上面的页脚结构(所需的设计),该结构具有两个宽度为50%,fb和google图片的按钮。但是我不能设置图像的高度。请帮帮我

这是我尝试过的代码

<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>

2 个答案:

答案 0 :(得分:0)

您可以像这样在代码中添加 HeightRequest

XAML

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

更多信息:

1,您的代码包含StackLayout,它也可以修改结果。

2,如果要进一步控制图像的大小和位置,可以向其添加轻击手势识别器。

.CS

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

答案 1 :(得分:0)

解决方法

1。您可以将Image控制与手势一起使用:

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