使用LAYOUT_FULLSCREEN或LAYOUT_STABLE UI 标志时

因此,当我强制我的应用程序进入浅色主题时,我的应用程序没有将其图标更改为深色,因此我遇到了一个问题。status bar

我发现了问题的根源,即当我拥有标志并应用于 .当我删除这两个标志时,中的图标是适当的黑暗。status barSYSTEM_UILAYOUT_STABLELAYOUT_FULLSCREENActivitystatus bar

但是我对上面的这个“解决方案”的问题是,我使用2个标志,以便使我的活动内容滚动到下面,我已经使它半透明。我还没有想出另一种方法来使我接受透明,并在其下方滚动内容,除了使用我目前拥有的2标志,这些标志再次和。SYSTEM_UIstatus barstatus barSYSTEM_UIActivitySYSTEM_UI_FLAG_LAYOUT_STABLESYSTEM_UI_FLAG_LAYOUT_FULLSCREEN

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

也许有人可以向我展示一种可靠的方法来让我接受透明度,并让内容滚动到它下面,而不必使用标志?这可能会解决我的问题,我认为...status barSYSTEM_UI

我能想到的唯一与共享相关的代码是这样的:

在我的主活动.java中,我有这个集合:

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

在我的风格中.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>

有什么想法吗?


弑天下
浏览 75回答 1
1回答

蝴蝶刀刀

好吧,所以我自己想通了。看起来我可能需要在调用之前设置标志。虽然,它可能是这些变化的组合,使它对我有用 - 我不完全确定。SYSTEM_UIsetContentView我更改的是使用这些标志,然后调用:setContentViewif (lightMode) {&nbsp; &nbsp; &nbsp; &nbsp; setTheme(R.style.AppTheme_Light_NoActionBar);&nbsp; &nbsp; &nbsp; &nbsp; getWindow().getDecorView().setSystemUiVisibility&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;(View.SYSTEM_UI_FLAG_LIGHT_NAVIGATION_BAR |&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR |&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN |&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; View.SYSTEM_UI_FLAG_LAYOUT_STABLE);&nbsp; &nbsp; }lightMode上面是我设置的布尔值,如果用户在我的应用中选择了按钮(对于深色模式/主题的工作方式相同)。SharedPreferenceChange themetoolbar最后,对于我的主题,我在我的(对于API 27及更高版本 - 对于较低的Android API看起来有点不同)MainActivitystyles.xmlstyles.xml<!-- Toolbar/NoActionBar variant of default Light Theme --><style name="AppTheme_Light_NoActionBar" parent="Theme.AppCompat.Light.NoActionBar">&nbsp; &nbsp; <item name="colorPrimary">@color/pure_white_transparent</item>&nbsp; &nbsp; <item name="colorPrimaryDark">@color/pure_white_transparent</item>&nbsp; &nbsp; <item name="colorAccent">@color/colorAccent</item>&nbsp; &nbsp; <item name="android:windowActionBarOverlay">true</item>&nbsp; &nbsp; <item name="android:windowTranslucentStatus">false</item>&nbsp; &nbsp; <item name="android:windowDrawsSystemBarBackgrounds">true</item>&nbsp; &nbsp; <item name="android:statusBarColor">@color/pure_white_transparent</item>&nbsp; &nbsp; <item name="android:navigationBarColor">@color/pure_white_transparent</item></style>因为在我看来,将标志设置为 并使得坐在 下面,我通过以下方式设置了一些适当的填充:SYSTEM_UILAYOUT_FULLSCREENLAYOUT_STABLEtoolbarstatusbarint statusBarHeight = getStatusBarHeight(this);其中 是用于解释不同 Android 设备上可能大小不同的方法(例如像素 3XL 的较大 )。getStatusBarHeightstatusbarsstatusbar我从StackOverflow上的其他地方得到了这个工作方法,但忘记了在哪里,所以如果有人知道,请随时链接它 - 下面的方法:&nbsp; &nbsp; // Gets the StatusBar's height of the particular display.&nbsp; &nbsp; public static int getStatusBarHeight(final Context context) {&nbsp; &nbsp; &nbsp; &nbsp; final Resources resources = context.getResources();&nbsp; &nbsp; &nbsp; &nbsp; final int resourceId = resources.getIdentifier("status_bar_height", "dimen", "android");&nbsp; &nbsp; &nbsp; &nbsp; if (resourceId > 0) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return resources.getDimensionPixelSize(resourceId);&nbsp; &nbsp; &nbsp; &nbsp; } else {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return (int) Math.ceil(24 * resources.getDisplayMetrics().density);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; } else {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return (int) Math.ceil(25 * resources.getDisplayMetrics().density);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; }}然后,我采用的值并将其用作编程的上边距:int statusBarHeighttoolbar// Setup the values for the toolbar's layout parameters.&nbsp; &nbsp; CoordinatorLayout.LayoutParams toolbarParams = new CoordinatorLayout.LayoutParams(&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; CoordinatorLayout.LayoutParams.MATCH_PARENT,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; CoordinatorLayout.LayoutParams.WRAP_CONTENT&nbsp; &nbsp; );&nbsp; &nbsp; toolbarParams.setMargins(0, statusBarHeight, 0, 0);&nbsp; &nbsp; // Find, Assign, and Setup the Toolbar to take place of the Actionbar.&nbsp; &nbsp; // Then inflate the proper menu to be used on-top of it, and set its layout parameters&nbsp; &nbsp; // which have been set in the code above.&nbsp; &nbsp; Toolbar toolbar = findViewById(R.id.toolbar);&nbsp; &nbsp; setSupportActionBar(toolbar);&nbsp; &nbsp; toolbar.inflateMenu(R.menu.menu_main);&nbsp; &nbsp; getSupportActionBar().setDisplayShowTitleEnabled(true);&nbsp; &nbsp; toolbar.setLayoutParams(toolbarParams);最后,我已确保我的应用程序正确位于 和 的下方,这是我通过设置适当的填充来实现的,也是以编程方式完成的:RecyclerViewstatusbartoolbar// Get and set the values for the OffsetPadding for the RecyclerView to int.&nbsp; &nbsp; int itemOffsetPaddingSide = (int) getResources().getDimension(R.dimen.item_offset);&nbsp; &nbsp; int actionBarSize = (int) getResources().getDimension(R.dimen.actionbarSizeWithExtraPadding);&nbsp; &nbsp; int itemOffsetPaddingTop = actionBarSize + statusBarHeight;&nbsp; &nbsp; // Set all the OffsetPadding values to the RecyclerView programmatically, so that&nbsp; &nbsp; // all the UI elements are padding properly, taking into account the devices screen&nbsp; &nbsp; // density/size/resolution!&nbsp; &nbsp; mRecyclerView.setPadding(itemOffsetPaddingSide, itemOffsetPaddingTop, itemOffsetPaddingSide, itemOffsetPaddingSide);的值是 ,for it 和 for ,通过组合和 ,我们得到加号,无论特定设备有多高(因为在帖子后面的代码中检索和分配的值)。int itemOffsetPaddingSide4dpint actionBarSize60dpint itemOffsetPaddingTopactionBarSizestatusBarHeight56dpstatusBarstatusBarHeight这一切都允许 我的内容舒适地位于 和 的下方,并且在滚动它们下面时也是可见的,因为两者都有 .RecyclerViewstatusbartoolbarbars90% opacity我已经独立学习Java编程和Android应用程序开发近2年了,虽然我为我这次取得的成就感到自豪,但我也不确定这是否是在我的应用程序中实现这种效果的最优雅方式。但无论哪种方式,它都可以工作,如果你发现它对你的应用程序也很有用,请告诉我!
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java