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

MAUI中如何调整Button内PNG图标大小?需求按钮58x38、图标14x14

解决方法

针对你需要创建58x38尺寸、内部嵌入14x14图标的按钮需求,提供以下几种可行方案:

方案一:利用Button的Content属性(Xamarin.Forms 5.0+)

Xamarin.Forms 5.0及以上版本的Button支持通过Content属性嵌套自定义内容,直接实现图标尺寸控制:

<Button 
    BackgroundColor="{StaticResource cBlue6}"  
    WidthRequest="58"
    HeightRequest="38"
    HorizontalOptions="Center"
    VerticalOptions="Center">
    <Button.Content>
        <Image 
            Source="error.png" 
            WidthRequest="14" 
            HeightRequest="14" 
            HorizontalOptions="Center" 
            VerticalOptions="Center"/>
    </Button.Content>
</Button>

方案二:自定义ContentView模拟按钮(兼容旧版本)

如果你的项目基于Xamarin.Forms 5.0以下版本,可使用ContentView封装可点击容器,模拟按钮效果:

<ContentView 
    WidthRequest="58"
    HeightRequest="38"
    BackgroundColor="{StaticResource cBlue6}"
    HorizontalOptions="Center"
    VerticalOptions="Center">
    <ContentView.GestureRecognizers>
        <!-- 绑定点击命令或事件 -->
        <TapGestureRecognizer Command="{Binding YourButtonCommand}" />
    </ContentView.GestureRecognizers>
    <Image 
        Source="error.png" 
        WidthRequest="14" 
        HeightRequest="14" 
        HorizontalOptions="Center" 
        VerticalOptions="Center"/>
</ContentView>

方案三:Grid包裹ImageButton

通过Grid固定按钮整体尺寸,内部放置小尺寸ImageButton并设置透明背景,既保留按钮原生交互,又满足尺寸需求:

<Grid 
    WidthRequest="58"
    HeightRequest="38"
    BackgroundColor="{StaticResource cBlue6}"
    HorizontalOptions="Center"
    VerticalOptions="Center">
    <ImageButton 
        Source="error.png"
        WidthRequest="14"
        HeightRequest="14"
        HorizontalOptions="Center"
        VerticalOptions="Center"
        BackgroundColor="Transparent"/>
</Grid>

内容的提问来源于stack exchange,提问作者infinitesimal

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.25 17:42:42