【问题标题】:Splash Screen Based on time of Day基于时间的启动画面
【发布时间】:2018-08-13 17:19:50
【问题描述】:

是否可以根据一天中的时间有不同的启动画面。我尝试创建 values-night/styles.xml 和 values-notnight/styles.xml 以及具有相同命名图像的 drawable-night 和 drawable-notnight,但它不会根据手机的时间交替。

有没有人有过在主要活动中不使用计时器而拥有不同启动屏幕的经验?

因此,仅在加载主要活动之前执行启动屏幕。

我是否缺少设置步骤?

values-notnight/styles.xml

<resources>

    <!-- Base application theme. -->
    <style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
        <!-- 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:windowBackground">@drawable/metalic</item>

    </style>
    <style name="AppTheme.Launcher">
        <item name="android:windowBackground">@drawable/launch_screen</item>
        <!-- Optional, on Android 5+ you can modify the colorPrimaryDark color to match the windowBackground color for further branding-->
        <!-- <item name="colorPrimaryDark">@android:color/white</item> -->
    </style>
    <style name="AppTheme.NoActionBar">
        <item name="windowActionBar">false</item>
        <item name="windowNoTitle">true</item>
    </style>

    <style name="AppTheme.AppBarOverlay" parent="ThemeOverlay.AppCompat.Dark.ActionBar" />

    <style name="AppTheme.PopupOverlay" parent="ThemeOverlay.AppCompat.Light" />

</resources>

值-notnight/styles.xml

<!-- Base application theme. -->
<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
    <!-- 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:windowBackground">@drawable/carbon</item>

</style>
<style name="AppTheme.Launcher">
    <item name="android:windowBackground">@drawable/launch_screen</item>
    <!-- Optional, on Android 5+ you can modify the colorPrimaryDark color to match the windowBackground color for further branding-->
    <!-- <item name="colorPrimaryDark">@android:color/white</item> -->
</style>
<style name="AppTheme.NoActionBar">
    <item name="windowActionBar">false</item>
    <item name="windowNoTitle">true</item>
</style>

<style name="AppTheme.AppBarOverlay" parent="ThemeOverlay.AppCompat.Dark.ActionBar" />

<style name="AppTheme.PopupOverlay" parent="ThemeOverlay.AppCompat.Light" />

drawable-notnight/launch_screen.xml

<!-- The background color, preferably the same as your normal theme -->
<item android:drawable="@drawable/carbon"/>
<!-- Your product logo - 144dp color version of your app icon -->
<item>
    <bitmap

        android:src="@drawable/logo"
        android:gravity="center"
        android:scaleGravity="center_vertical|center_horizontal"
        android:scaleHeight="60%"
        android:scaleWidth="60%"/>


</item>

drawable-night/launch_screen.xml

<!-- The background color, preferably the same as your normal theme -->
<item android:drawable="@drawable/metalic"/>
<!-- Your product logo - 144dp color version of your app icon -->
<item>
    <bitmap

        android:src="@drawable/logo"
        android:gravity="center"
        android:scaleGravity="center_vertical|center_horizontal"
        android:scaleHeight="60%"
        android:scaleWidth="60%"/>


</item>

AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.root.myapplication">
    <uses-permission android:name="android.permission.CAMERA" />
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
    <uses-feature android:name="android.hardware.camera" />
    <application
        android:allowBackup="true"
        android:icon="@drawable/tech"
        android:label="@string/app_name"
        android:roundIcon="@drawable/tech"
        android:supportsRtl="true"
        android:theme="@style/AppTheme.Launcher">
        <activity
            android:name=".MainActivity"
            android:label="@string/app_name"
            android:theme="@style/AppTheme.Launcher">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

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

</manifest>

【问题讨论】:

  • 如果您正确地从手机中获取时间,当然您可以根据该信息启动不同的启动画面。例如,通过定义具有相应布局的单独 Splash 类
  • 发布您的代码。
  • 我添加了代码,目前它默认为drawable-notnight代码,即使我手机上的时间已手动设置为23:46。
  • 我的清单需要额外的权限吗?

标签: android android-layout android-studio android-drawable android-styles


【解决方案1】:

你必须做的三件事

第一:白天,文件夹名称为values,晚上,文件夹名称为values-night。可相应地绘制drawabledrawable-night

第二: 使用Theme.AppCompat.DayNight.DarkActionBar 作为父主题。

第三:在你的布局中使用ViewFlipper

在您的代码中 查找当前时间并检查预期时间:

  ViewFlipper vf = (ViewFlipper) findViewById( R.id.viewFlipper );
  if (mCurrentTime == //whatever time you want) {
        vf.showNext();
  }

【讨论】:

  • 我做了这些更改,我的设备上的时间是晚上 10:05,但它仍然显示白天模式。我还有什么需要做的吗?
  • ViewFlipper 不适用于闪屏,因为闪屏是在 Activity 选择布局之前从清单中出现的。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2022-12-28
  • 1970-01-01
  • 2018-11-20
  • 1970-01-01
  • 1970-01-01
  • 2011-03-02
相关资源
最近更新 更多