You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

DotNet Maui:使用ContentLayout时如何调整Button的ImageSource大小?

问题:使用ContentLayout将文本放在ImageSource下方时,如何调整图像大小?

初始代码

<HorizontalStackLayout>
    <Button MaximumHeightRequest="50"
            Text="Open"
            VerticalOptions="Start"
            ImageSource="file_open.png"
            ContentLayout="left,50" />
    <Button Text="Open file test"
            ContentLayout="Top,0"
            VerticalOptions="Start"
            ImageSource="file_open.png"
            Command="{Binding DivideBy2Command}" />
    <ImageButton x:Name="Toto"
            MaximumHeightRequest="50"
            VerticalOptions="Start"
            Source="file_open.png"
            Command="{Binding DivideBy2Command}" />
</HorizontalStackLayout>

初始效果

初始效果截图

解决方案

结合StackOverflow用户ToolmakerSteve和Alexandar May - MSFT的建议,通过调整Padding、HeightRequest、ContentLayout等属性,可实现图像大小调整并达到预期布局效果。以下是更新后的代码:

<HorizontalStackLayout>
    <Button Text="Open"
            HeightRequest="50"
            VerticalOptions="Start"
            ImageSource="file_open.png"
            ContentLayout="left, 0"
            />
    <Button 
        VerticalOptions="Start"
        Text="Open File"
        FontSize="10"
        ImageSource="file_open.png"
        ContentLayout="Top,-10"
        Padding="2,-10,2,2"
        HeightRequest="50"
        WidthRequest="50"
        />
    <ImageButton 
        VerticalOptions="Start"
        Source="file_open.png"
        MaximumHeightRequest="50"  
        />
</HorizontalStackLayout>

优化后效果

优化后效果截图

内容的提问来源于stack exchange,提问作者Jean-Marc Flamand

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.11 19:45:29