【问题标题】:Android windowBackground in theme not working - Splashscreen主题中的Android windowBackground不起作用 - 启动画面
【发布时间】:2018-07-20 13:10:30
【问题描述】:

我正在尝试向我的 Android 应用添加启动画面。

我一直遵循here 的指示。这是我理解的标准方式,而且看起来合乎逻辑。

所以真的很简单吧?创建可绘制资源,在主题中使用它作为windowBackground,使用主题?好吧,这对我不起作用...

清单:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="euroicc.sicc">

    <uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
    <uses-permission android:name="android.permission.INTERNET"/>

    <application
        android:allowBackup="true"
        android:icon="@drawable/app_icon"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true"
        android:theme="@style/AppTheme.Splashscreen">
        <activity android:name="euroicc.sicc.ui.MainActivity"
            android:theme="@style/AppTheme.Splashscreen">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

</manifest>

风格:

<resources>

    <style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
        <!-- Customize your theme here. -->
        <item name="colorPrimary">@color/colorPrimary</item>
        <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
        <item name="colorAccent">@color/colorAccent</item>
        <item name="android:spinnerDropDownItemStyle">@style/mySpinnerItemStyle</item>
        <item name="android:itemBackground">@color/main_background</item>
        <item name="android:textColor">@color/text_default</item>


       <!--<item name="android:popupMenuStyle">@style/MyAlertDialog</item>-->
    </style>

    <style name="AppTheme.Splashscreen" parent="AppTheme">
        <item name="android:windowTranslucentStatus">true</item>
        <item name="android:windowTranslucentNavigation">true</item>

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

</resources>

splashscreen_drawable:

<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
    <item>
        <shape android:shape="rectangle">
            <solid android:color="@android:color/holo_green_light"></solid>
        </shape>
    </item>
    <item>
        <bitmap android:src="@drawable/splashschreen_eicc"
            android:gravity="center"></bitmap>
    </item>
</layer-list>

我尝试了其他网站的其他代码,但似乎没有任何效果。

仅白色背景显示不到一秒钟(大约),然后应用程序开始运行。我在主要活动的 onCreate 方法中设置了主题 AppTheme,因此它始终使用它的默认主题,但开始时除外。

我还覆盖了 onPause 和 onResume,但我称它们为超级,所以这应该不是问题。

我正在 v7.0.0 上进行测试,但这也应该不是问题?

我有更多样式,但我从示例代码中删除了它们,因为它们用于 xml 文件中的对话框和元素,而不是用于活动。

那么问题是什么,我该如何解决?当我第一次阅读启动画面时,我认为设置 windowBackground 就足够了

【问题讨论】:

  • 我试过你的代码,它工作正常。尝试清理 -> 构建
  • @RahulKumar 没用):
  • 在 API 21 上测试了您的代码,运行良好。唯一有点不寻常的是,我只为活动设置AppTheme.Splashscreen(为应用程序设置AppTheme)。无论如何,我想知道哪里出了问题,因为这正是我使用的方法
  • @bwt 是的,我知道,但我很绝望,所以我把它放在清单中的任何地方。无论哪种方式,我在 Main Activity 的 onCreate 上设置了不同的主题,因此应用程序使用正确的主题。
  • 所以...呃...它现在正在工作。我真的什么也没做。只是周末下班。我真的很困惑为什么以前会发生这种情况,但现在没关系。

标签: android user-interface splash-screen


【解决方案1】:

您没有在styles.xml 中设置背景,而是在您的splashcreen 布局xml 文件中设置。这是我的布局示例:

<?xml version="1.0" encoding="utf-8"?>

xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:ignore="ContentDescription"
android:background="@drawable/green"      ==============================> HERE!
tools:context=".activity_splash_screen">


<ImageView
    android:id="@+id/logo"
    android:layout_width="@dimen/_150sdp"
    android:layout_height="@dimen/_150sdp"
    android:layout_alignParentTop="true"
    android:layout_centerHorizontal="true"
    android:layout_marginTop="@dimen/_100sdp"
    android:src="@drawable/white_logo"
     />

<ImageView
    android:id="@+id/title"
    android:layout_width="@dimen/_240sdp"
    android:layout_height="@dimen/_150sdp"
    android:layout_alignParentBottom="true"
    android:layout_centerHorizontal="true"
    android:layout_marginBottom="@dimen/_100sdp"
    android:src="@drawable/title"
   />

如果您需要有关整个代码的帮助,我可以帮助您!

【讨论】:

  • 我没有用于启动画面的布局文件,但在 drawables“splashscreen_drawable”中有一个 xml 文件。我更喜欢这种方式,不使用布局,或者只使用布局、样式(主题)、其他 xml 文件和 ofc manifest.xml。
【解决方案2】:

我遇到过这个问题。我将初始屏幕创建为可绘制的图层列表,并在主题中创建为 windowBackground,因为它在任何地方都被推荐,但它不适用于某些设备(如我和我朋友的摩托罗拉,其他一些手机) - 黑屏将显示。对我来说,解决方案是将我的启动活动代码包装在 Handler 的 postDelayed 可运行文件中,并有一些小的延迟:

override fun onCreate(savedInstanceState: Bundle?) {

    super.onCreate(savedInstanceState)

    Handler(Looper.getMainLooper()).postDelayed( { //this is a trick so the splash screen - in window bg theme - is displayed on all devices, instead of black screen. E.g. moto g6
        val startMainActivityIntent = ...
        startActivity(startMainActivityIntent)
        finish()
    }, 100)
}

我不是特别喜欢延迟 UI,如果我知道更好的方法,我不会这样做。但总比黑屏好。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2022-12-20
    • 1970-01-01
    • 2019-11-13
    • 2014-11-19
    • 2018-02-14
    • 2017-06-15
    • 1970-01-01
    • 2020-01-23
    相关资源
    最近更新 更多