为了账号安全,请及时绑定邮箱和手机立即绑定

使用LAYOUT_FULLSCREEN或LAYOUT_STABLE UI 标志时

使用LAYOUT_FULLSCREEN或LAYOUT_STABLE UI 标志时

弑天下 2022-09-22 10:44:49
因此,当我强制我的应用程序进入浅色主题时,我的应用程序没有将其图标更改为深色,因此我遇到了一个问题。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>有什么想法吗?
查看完整描述

1 回答

?
蝴蝶刀刀

TA贡献1801条经验 获得超8个赞

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


我更改的是使用这些标志,然后调用: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上面是我设置的布尔值,如果用户在我的应用中选择了按钮(对于深色模式/主题的工作方式相同)。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">

    <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_UILAYOUT_FULLSCREENLAYOUT_STABLEtoolbarstatusbar


int statusBarHeight = getStatusBarHeight(this);其中 是用于解释不同 Android 设备上可能大小不同的方法(例如像素 3XL 的较大 )。getStatusBarHeightstatusbarsstatusbar


我从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 statusBarHeighttoolbar


// 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);

最后,我已确保我的应用程序正确位于 和 的下方,这是我通过设置适当的填充来实现的,也是以编程方式完成的:RecyclerViewstatusbartoolbar


// 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);

的值是 ,for it 和 for ,通过组合和 ,我们得到加号,无论特定设备有多高(因为在帖子后面的代码中检索和分配的值)。int itemOffsetPaddingSide4dpint actionBarSize60dpint itemOffsetPaddingTopactionBarSizestatusBarHeight56dpstatusBarstatusBarHeight


这一切都允许 我的内容舒适地位于 和 的下方,并且在滚动它们下面时也是可见的,因为两者都有 .RecyclerViewstatusbartoolbarbars90% opacity


我已经独立学习Java编程和Android应用程序开发近2年了,虽然我为我这次取得的成就感到自豪,但我也不确定这是否是在我的应用程序中实现这种效果的最优雅方式。但无论哪种方式,它都可以工作,如果你发现它对你的应用程序也很有用,请告诉我!


查看完整回答
反对 回复 2022-09-22
  • 1 回答
  • 0 关注
  • 72 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信