首页 > 解决方案 > 为什么在切换到新主题后仍然使用 android:background 的值

问题描述

我在 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:windowBackgroundinTheme.Splash代替,因为这会导致我的自定义启动页面移动到状态栏下方。

标签: androidxamarinxamarin.android

解决方案


只需android:background在 AppTheme 中覆盖。


推荐阅读