使用LAYOUT_FULLSCREEN或LAYOUT_STABLE UI标志时,Light状态栏图标不会变暗?

问题描述 投票:0回答:1

因此,当我强迫我的应用程序进入Light Theme时,我的应用程序的status bar没有将其图标更改为黑暗。

我已经发现问题的根源,即status bar中的图标不希望变暗,当我将SYSTEM_UI标志LAYOUT_STABLELAYOUT_FULLSCREEN应用于Activity时。当我删除这两个标志时,status bar中的图标是正确的黑色。

但我上面这个“解决方案”的问题是我使用2个SYSTEM_UI标志,以便让我的Activity的内容滚动到status bar下面,我已经做了半透明。我没有想出另一种方法让我的status bar接受透明度并且内容滚动在它下面,而不是使用我现在为这个SYSTEM_UI的2个Activity标志,再次是SYSTEM_UI_FLAG_LAYOUT_STABLESYSTEM_UI_FLAG_LAYOUT_FULLSCREEN

有没有人知道如何解决这个问题?

也许有人可以告诉我一个可靠的方法让我的status bar接受透明度并让内容在其下方滚动而不必使用SYSTEM_UI标志?这可能会解决我的问题,我想......

我能想到的唯一与代码相关的代码是:

在我的MainActivity.java中,我有这个集合:

getWindow().getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_STABLE | View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN);`

在我的styles.xml中,我为Light Theme设置了这个主题:

<!-- Toolbar/NoActionBar variant of default Light Theme -->
<style name="AppTheme_Light_NoActionBar" parent="Theme.AppCompat.Light.NoActionBar">
    <item name="colorPrimary">@color/pure_white_transparent</item>
    <item name="colorPrimaryDark">@color/pure_white_transparent</item>
    <item name="colorAccent">@color/colorAccent</item>
    <item name="android:windowActionBarOverlay">true</item>
    <item name="android:windowTranslucentStatus">false</item>
    <item name="android:windowDrawsSystemBarBackgrounds">true</item>
    <item name="android:statusBarColor">@color/pure_white_transparent</item>
    <item name="android:windowLightStatusBar">true</item>
    <item name="android:navigationBarColor">@color/pure_white</item>
    <item name="android:windowLightNavigationBar">true</item>
</style>

有任何想法吗?

java android android-xml android-statusbar
1个回答
0
投票

好的,所以我自己想出来了。看起来我可能需要在调用SYSTEM_UI之前设置setContentView标志。虽然,它可能是这些变化的组合,使它对我有用 - 我不完全确定。

我改变的是在调用setContentView之前使用这些标志:

if (lightMode) {
        setTheme(R.style.AppTheme_Light_NoActionBar);
        getWindow().getDecorView().setSystemUiVisibility
               (View.SYSTEM_UI_FLAG_LIGHT_NAVIGATION_BAR |
                View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR | 
                View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN |
                View.SYSTEM_UI_FLAG_LAYOUT_STABLE);
    }

如果用户在我的应用程序的lightMode中选择了SharedPreference按钮(对于黑暗模式/主题的工作方式相同),上面的Change theme是我设置的toolbar布尔值。

最后,对于我的MainActivity的主题,我在我的styles.xml中设置了这个样式(对于API 27及更高版本 - 对于较低的Android API'styles.xml看起来有点不同):

<!-- Toolbar/NoActionBar variant of default Light Theme -->
<style name="AppTheme_Light_NoActionBar" parent="Theme.AppCompat.Light.NoActionBar">
    <item name="colorPrimary">@color/pure_white_transparent</item>
    <item name="colorPrimaryDark">@color/pure_white_transparent</item>
    <item name="colorAccent">@color/colorAccent</item>
    <item name="android:windowActionBarOverlay">true</item>
    <item name="android:windowTranslucentStatus">false</item>
    <item name="android:windowDrawsSystemBarBackgrounds">true</item>
    <item name="android:statusBarColor">@color/pure_white_transparent</item>
    <item name="android:navigationBarColor">@color/pure_white_transparent</item>
</style>

因为在我看来,将SYSTEM_UI标志设置为LAYOUT_FULLSCREENLAYOUT_STABLE使得toolbar位于statusbar下方,我通过以下方式设置了一些合适的填充:

int statusBarHeight = getStatusBarHeight(this); getStatusBarHeight是一种用于解释不同Android设备上可能不同大小的statusbars的方法(例如Pixel 3XL更大的statusbar)。

我从StackOverflow上的其他地方得到了这个工作方法,但忘了在哪里,所以如果有人知道,请随意链接它 - 方法如下:

    // Gets the StatusBar's height of the particular display.
    public static int getStatusBarHeight(final Context context) {
        final Resources resources = context.getResources();
        final int resourceId = resources.getIdentifier("status_bar_height", "dimen", "android");
        if (resourceId > 0) {
            return resources.getDimensionPixelSize(resourceId);
        } else {
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
                return (int) Math.ceil(24 * resources.getDisplayMetrics().density);
            } else {
                return (int) Math.ceil(25 * resources.getDisplayMetrics().density);
            }
        }
}

然后我取了int statusBarHeight的值并以编程方式将其用作我的toolbar的上边距:

// Setup the values for the toolbar's layout parameters.
    CoordinatorLayout.LayoutParams toolbarParams = new CoordinatorLayout.LayoutParams(
            CoordinatorLayout.LayoutParams.MATCH_PARENT,
            CoordinatorLayout.LayoutParams.WRAP_CONTENT
    );
    toolbarParams.setMargins(0, statusBarHeight, 0, 0);

    // Find, Assign, and Setup the Toolbar to take place of the Actionbar.
    // Then inflate the proper menu to be used on-top of it, and set its layout parameters
    // which have been set in the code above.
    Toolbar toolbar = findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);
    toolbar.inflateMenu(R.menu.menu_main);
    getSupportActionBar().setDisplayShowTitleEnabled(true);
    toolbar.setLayoutParams(toolbarParams);

最后,我确保我的应用程序的RecyclerView正确地坐在statusbartoolbar下面,我通过设置适当的填充来完成它,也是以编程方式:

// Get and set the values for the OffsetPadding for the RecyclerView to int.
    int itemOffsetPaddingSide = (int) getResources().getDimension(R.dimen.item_offset);
    int actionBarSize = (int) getResources().getDimension(R.dimen.actionbarSizeWithExtraPadding);
    int itemOffsetPaddingTop = actionBarSize + statusBarHeight;
    // Set all the OffsetPadding values to the RecyclerView programmatically, so that
    // all the UI elements are padding properly, taking into account the devices screen
    // density/size/resolution!
    mRecyclerView.setPadding(itemOffsetPaddingSide, itemOffsetPaddingTop, itemOffsetPaddingSide, itemOffsetPaddingSide);

int itemOffsetPaddingSide的值是4dp,对于int actionBarSize它是60dpint itemOffsetPaddingTop,通过组合actionBarSizestatusBarHeight,我们得到56dp加上特定设备的statusBar高(因为检索到的值并在代码中进一步分配给statusBarHeight)这个帖子)。

这一切都允许我的RecyclerView的内容舒适地坐在statusbartoolbar之下,并且当在两个下面滚动时也是可见的,因为bars都有90% opacity

我已经独立地研究Java编程和Android应用程序开发近2年了,虽然我为这次来到的地方感到自豪,但我也不确定这是实现这种效果的最优雅方式在我的应用程序中但无论哪种方式,它都有效,如果您发现它对您的应用程序也有用,请告诉我!

© www.soinside.com 2019 - 2024. All rights reserved.