【问题标题】:Labels appear in Android emulator but not on physical device标签出现在 Android 模拟器中,但不在物理设备上
【发布时间】:2021-05-03 10:10:25
【问题描述】:

我使用 Xamarin.Forms 创建了一个导航侧边栏应用程序,甚至在我对项目进行任何更改之前,我就发现出了点问题。当我在计算机上启动 Pixel 2 模拟器时,我看到以下 正确 视图。

Emulator picture

但是,当我发送 APK 或将其附加到我的计算机并调试程序时,我注意到标签没有出现,如下面我的 OnePlus 8T + 5G (Android 11) 上的屏幕截图所示。

Phone screenshot

我的手机和模拟器之间显然有很多不同之处,但在大多数情况下,所有设备上的差异应该是相同的,并且应用程序确实加载了其他元素(如按钮和图像)正确。这是怎么回事?

我尝试过的事情

  • 搜索其他帖子,例如 this one,在模拟器和手机上显示不同的结果。不走运。
  • 在 Debug/Release 和更改 APK 首选项之间切换以避免代码压缩和可能的数据丢失。不走运。

这是代码。

<?xml version="1.0" encoding="utf-8" ?>
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
             xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
             x:Class="Car_Scan.Views.BrowsePage"
             xmlns:vm="clr-namespace:Car_Scan.ViewModels"
             Title="{Binding Title}">
    
    <ContentPage.BindingContext>
        <vm:AboutViewModel />
    </ContentPage.BindingContext>
    
    <ContentPage.Resources>
        <ResourceDictionary>
            <Color x:Key="Accent">#96d1ff</Color>
        </ResourceDictionary>
    </ContentPage.Resources>

    <Grid>
        <Grid.RowDefinitions>
            <RowDefinition Height="Auto" />
            <RowDefinition Height="*" />
        </Grid.RowDefinitions>
        <StackLayout BackgroundColor="{StaticResource Accent}" VerticalOptions="FillAndExpand" HorizontalOptions="Fill">
            <StackLayout Orientation="Horizontal" HorizontalOptions="Center" VerticalOptions="Center">
                <ContentView Padding="0,40,0,40" VerticalOptions="FillAndExpand">
                    <Image Source="xamarin_logo.png" VerticalOptions="Center" HeightRequest="64" />
                </ContentView>
            </StackLayout>
        </StackLayout>
        <ScrollView Grid.Row="1">
            <StackLayout Orientation="Vertical" Padding="30,24,30,24" Spacing="10">
                <Label Text="Start developing now" FontSize="Title"/>
                <Label Text="Make changes to your XAML file and save to see your UI update in the running app with XAML Hot Reload. Give it a try!" FontSize="16" Padding="0,0,0,0"/>
                <Label FontSize="16" Padding="0,24,0,0">
                    <Label.FormattedText>
                        <FormattedString>
                            <FormattedString.Spans>
                                <Span Text="Learn more at "/>
                                <Span Text="https://aka.ms/xamarin-quickstart" FontAttributes="Bold"/>
                            </FormattedString.Spans>
                        </FormattedString>
                    </Label.FormattedText>
                </Label>
                <Button Margin="0,10,0,0" Text="Learn more"
                        Command="{Binding OpenWebCommand}"
                        BackgroundColor="{StaticResource Primary}"
                        TextColor="White" />
            </StackLayout>
        </ScrollView>
    </Grid>

</ContentPage>

【问题讨论】:

  • 你有没有想过这个问题?我有一个非常相似的问题。

标签: android xamarin.forms android-emulator


【解决方案1】:

我遇到了同样的问题。我能够通过创建一个新的 Xamarin.Forms 项目并按原样运行来重现。在我的手机上,文本是不可见的,但在模拟器上运行时它是可见的。在尝试了几种不同的方法后,我意识到我的手机设置为暗模式。我转向灯光模式,它起作用了。我仍然不确定暗模式到底是什么导致了这种情况发生,因为我已经将手机设置为暗模式很长时间了。我找到了this ,我将尝试实现它,看看它是否允许我在明暗模式之间无缝切换。 希望对您有所帮助

【讨论】:

    【解决方案2】:

    尝试将TextColor 属性设置为一种颜色。像这样:

    <Label Text="Start developing now" FontSize="Title" TextColor="Black"/>
                                                        ^ this
    

    【讨论】:

      猜你喜欢
      • 2021-06-28
      • 1970-01-01
      • 2023-03-09
      • 2021-07-19
      • 2011-04-12
      • 1970-01-01
      • 1970-01-01
      • 2020-07-15
      相关资源
      最近更新 更多