首页 > 解决方案 > 图像在 xaml 页面和 Android drawable 上的中心不同

问题描述

我有一个可绘制的启动画面和一个启动页面。

可绘制对象设置为 MainActivity 主题,因此它是您打开应用程序时首先看到的内容。

然后,该应用程序导航到 SplashPage,它在后台运行一些代码。

我在两个屏幕的中心放置了一个图像。有身高差。我希望两个屏幕相同。

在此处输入图像描述

splash_screen.xml:

<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
    <item>
        <color android:color="@color/splash_background"/>
    </item>
    <item
        android:height="400dp"
        android:width="400dp"
        android:gravity="center">
        <bitmap
            android:src="@drawable/circle"
            android:gravity="fill"/>
    </item>
</layer-list>

SplashPage.xaml

<?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="MyApp.Views.SplashPage"
             BackgroundColor="{Binding BackgroundColor}">
    <ContentPage.Content>
        <AbsoluteLayout VerticalOptions="FillAndExpand" HorizontalOptions="FillAndExpand">
            <Image Source="{Binding SplashImage}" WidthRequest="400" HeightRequest="400" Aspect="AspectFit" AbsoluteLayout.LayoutFlags="PositionProportional" AbsoluteLayout.LayoutBounds="0.5,0.5"/>
        </AbsoluteLayout>
    </ContentPage.Content>
</ContentPage>

标签: xamarinxamarin.formsxamarin.android

解决方案


这是因为当您android:windowBackground在主题中使用该属性时,其绘制高度包含 和 的高度StatusbarNavigationbar而在 Page.xaml 中绘制高度设置在两者之间,因此存在高度差异。

解决方案:

改变

<item name="android:windowBackground">@drawable/splash_screen</item>

<item name="android:background">@drawable/splash_screen</item>

推荐阅读