我在我的Xamarin.Android应用程序中的Style.xml中有这个:
<resources>
<!-- Splash styles -->
<style name="Theme.Splash" parent="Theme.AppCompat.Light.NoActionBar">
<item name="android:background">@drawable/splash_drawable</item>
<item name="android:windowNoTitle">true</item>
<item name="android:windowIsTranslucent">false</item>
<item name="android:windowIsFloating">false</item>
<item name="android:backgroundDimEnabled">true</item>
</style>
<!-- Theme -->
<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
<!-- window:background is not set here, but even if it set (to say, @null for eg), there is no change. The value from Theme.Splash is preserved. -->
....
我的MainActivity.cs看起来像这样:
[Activity(
Label = "My app",
Icon = "@drawable/icon",
Theme = "@style/Theme.Splash",
MainLauncher = true,
LaunchMode = LaunchMode.SingleTask,
ConfigurationChanges = ConfigChanges.Orientation | ConfigChanges.ScreenSize | ConfigChanges.Locale | ConfigChanges.LayoutDirection,
ScreenOrientation = ScreenOrientation.Portrait
)
]
public class MainActivity : FormsAppCompatActivity
{
protected override void OnCreate(Bundle bundle)
{
// Changing to App theme since we are in OnCreate and we are ready to
// "hide" the splash.
// But the android:background property value from Theme.Splash remains.
base.Window.RequestFeature(WindowFeatures.ActionBar);
base.SetTheme(Resource.Style.AppTheme);
...
如何删除为android:background
设置的内容?它导致了重大问题,因为当应用程序加载完毕后,它会重叠我的所有视图。
注意:我不能在android:windowBackground
中使用Theme.Splash
,因为这会导致我的自定义启动页面移动到状态栏下方。
只需在AppTheme中覆盖android:background
。
你提到你“不能在Theme.Splash中使用android:windowBackground,因为这会导致我的自定义启动页面移动到状态栏下方”。尝试添加:
<item name="android:windowFullscreen">true</item>
<item name="android:windowContentOverlay">@null</item>
<item name="android:windowBackground">@drawable/splash_drawable</item>
应该解决这个问题。确保您的drawable具有响应性,因此您不会遇到不同屏幕尺寸和配置的问题。
*********************更新*********************
尝试在应用程序标记中的manifest.xml中设置splash主题,并使用splashscreen活动,然后使用App主题启动MainActivity。您可以在splash活动中进行应用程序初始化(请记住,同步应用程序初始化应限于在用户可以访问UI之前必须完成的事情,其他所有内容应该异步完成并通过事件触发UI中的更改) 。我们几乎完全使用这种方法,因为我们通常在此处执行身份验证过程,并在启动时添加消息和进度条,例如“初始化”或“尝试进行身份验证”以保持用户参与。
这是我的意思的一个例子:
在manifest.xml中。包括:
<application android:label="@string/app_name" android:allowBackup="true" android:theme="@style/AppDark.Fullscreen.Splash" android:icon="@mipmap/ic_app_launcher">
…..
<application>
在样式.xml中包括:
<style name="AppTheme" parent="AppDark">
<style name="AppTheme.NoActionBar">
<item name="windowActionBar">false</item>
<item name="windowNoTitle">true</item>
<item name="windowActionModeOverlay">true</item>
<item name="android:windowTranslucentStatus">true</item>
<item name="android:windowDrawsSystemBarBackgrounds">true</item>
<item name="android:statusBarColor">@android:color/transparent</item>
</style>
<style name="AppDark" parent="Theme.AppCompat.Light.DarkActionBar">
<item name="android:windowNoTitle">true</item>
<item name="android:windowActionBar">false</item>
<item name="android:colorPrimary">@color/app_background</item>
<item name="android:backgroundDimEnabled">true</item>
</style>
<style name="AppLight" parent="Theme.AppCompat.Light.NoActionBar">
<item name="android:windowNoTitle">true</item>
<item name="android:windowActionBar">false</item>
<item name="android:colorPrimary">@color/app_background</item>
</style>
<style name="AppLight.Fullscreen">
<item name="android:windowFullscreen">true</item>
<item name="android:windowContentOverlay">@null</item>
</style>
<style name="AppLight.Fullscreen.Splash">
<item name="android:windowBackground">@drawable/responsive_splashscreen</item>
</style>
然后为活动:
[Activity(
Label = "@string/app_name",
MainLauncher = true,
Exported = true,
LaunchMode = LaunchMode.SingleTask,
ConfigurationChanges = ConfigChanges.Orientation | ConfigChanges.ScreenSize | ConfigChanges.Locale | ConfigChanges.LayoutDirection,
ScreenOrientation = global::Android.Content.PM.ScreenOrientation.Portrait)]
public class SplashActivity : BaseActivity
{
…….
[Activity(
Label = "@string/app_name",
Theme = "@style/AppTheme.NoActionBar"
ConfigurationChanges = ConfigChanges.Orientation | ConfigChanges.ScreenSize | ConfigChanges.Locale | ConfigChanges.LayoutDirection,
ScreenOrientation = global::Android.Content.PM.ScreenOrientation.Portrait,
WindowSoftInputMode = SoftInput.AdjustResize)]
public class MainActivity : BaseActivity
{
…….