【问题标题】:Image overflow in ListView Xamarin.Forms (Android)ListView Xamarin.Forms (Android) 中的图像溢出
【发布时间】:2020-03-04 01:30:50
【问题描述】:

有一个 ListView,其中对于 List 的每个元素,您有 3 个用于 3 个不同选项的按钮,当您进入屏幕时,ImageButton 的图像中没有溢出问题,但是当您进行第一次滚动时,源开始如下图所示溢出

上下滚动时图像会失去中心并且错位,iOS不会出现此问题,仅会出现在Android(版本6、7、8和9)中,显然是Android渲染的问题图片

我尝试将 ImageButton 控件更改为 Button 并且问题仍然存在,还尝试将“Aspect”属性分配给 ImageButton,但我没有得到成功的结果,有什么帮助如何解决这个问题吗?然后是我的代码 XAML

   <ListView 
                ItemsSource="{Binding ListaRecintos}"
                SelectionMode="None"
                IsRefreshing="{Binding IsRefreshing, Mode=TwoWay}"        
                             RowHeight="80"
                             HasUnevenRows="True">
                    <ListView.ItemTemplate>
                        <DataTemplate>
                            <ViewCell>
                                <StackLayout
                                    Margin="0,4,0,0"
                                Orientation="Vertical">

                                    <Grid>
                                        <Grid.ColumnDefinitions>
                                            <ColumnDefinition Width="1*"/>
                                            <ColumnDefinition Width="3.9*"/>
                                            <ColumnDefinition Width="*"/>
                                        </Grid.ColumnDefinitions>


                                        <Label
                                            Text="{Binding Code}" 
                                            HorizontalOptions="Start" 
                                            FontSize="Small" 
                                            WidthRequest="40"
                                            HeightRequest="30"
                                            FontAttributes="Bold"
                                            VerticalTextAlignment="Center"
                                            HorizontalTextAlignment="Center"
                                            TextColor="{StaticResource das.color.texto}"
                                            VerticalOptions="CenterAndExpand"
                                            Grid.Column="0">
                                        </Label>

                                        <StackLayout Orientation="Vertical"
                                                      Grid.Column="1">
                                             <Label                                 
                                            Text="{Binding Name}"
                                            HorizontalOptions="Start" 
                                            FontSize="Small" 
                                            HeightRequest="32"
                                            MaxLines="2"
                                            TextColor="{StaticResource das.color.texto}"
                                            VerticalOptions="Center"
                                            VerticalTextAlignment="Center">
                                        </Label>
                                        </StackLayout>
                                    </Grid>

                                    <!--STACK BUTTON-->
                                    <StackLayout
                                              Orientation="Horizontal"
                                              HorizontalOptions="EndAndExpand"
                                              Margin="0,1,1,0">

                                        <!--BUTTON 1-->
                                        <ImageButton 
                                            Source="ic_check_wt"
                                            Aspect="AspectFit"
                                            CornerRadius="0"
                                            BackgroundColor="{StaticResource das.color.estado_success}"                                            
                                            HorizontalOptions="End"
                                            VerticalOptions="Center"
                                            HeightRequest="35"
                                            WidthRequest="50"
                                            CommandParameter="2"
                                            Command="{Binding ControlAuditoriaCommand}"
                                            IsEnabled="{Binding EnabledButton}"/>

                                        <!--BUTTON 2-->
                                        <ImageButton 
                                            Source="ic_hammer"                                         
                                            BackgroundColor="{StaticResource das.color.estado_primary}"                                        
                                            HorizontalOptions="End"
                                            CornerRadius="0"
                                            Aspect="AspectFit"
                                            VerticalOptions="Center"
                                            HeightRequest="35"
                                            WidthRequest="50"
                                            CommandParameter="1"
                                            Command="{Binding ControlAuditoriaCommand}"
                                            IsEnabled="{Binding EnabledButton}"/>

                                        <!--BUTTON 3-->
                                        <ImageButton 
                                            Source="ic_arrow_up"
                                            BackgroundColor="{StaticResource das.color.estado_success}"                                           
                                            HorizontalOptions="End"
                                            CornerRadius="0"
                                            Aspect="AspectFit"
                                            VerticalOptions="Center"
                                            HeightRequest="35"
                                            WidthRequest="50"
                                            CommandParameter="3"
                                            Command="{Binding ControlAuditoriaCommand}"
                                            IsEnabled="{Binding EnabledButton}"/>

                                    </StackLayout>
                                </StackLayout>
                            </ViewCell>
                        </DataTemplate>
                    </ListView.ItemTemplate>
                </ListView>

如何避免这种行为?对我有帮助吗?

【问题讨论】:

  • 您的表单版本是多少?预期的图像是什么?如果可能,您能否提供一个基本项目供我们测试?
  • 我的 Xamarin.Forms 版本是 4.3.0.947036 我期望的图像是居中而不变形的图像,不幸的是我无法提供有问题的项目,因为它来自我工作的公司帮帮我? @ColeXia-MSFT
  • 我建议你使用 (Frame + image ) 而不是 ImageButton ,并在其上添加 Tapgesture 。
  • 你的解决方案是正确的!应用它以将其标记为正确@ColeXia-MSFT
  • 按钮怎么会覆盖Frame的整个区域呢?当我放置框架+按钮时,按钮和框架之间的空间太大,我能解释一下吗? @ColeXia-MSFT

标签: android image xamarin xamarin.forms xamarin.android


【解决方案1】:

我建议你使用 (Frame + image) 而不是 ImageButton ,并在其上添加 tapgesture 。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-09-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-10-25
    • 2014-09-26
    • 1970-01-01
    相关资源
    最近更新 更多