将我的工具栏图像显示为状态栏的背景,从而影响android中的导航栏 [英] displaying my toolbar image as background to status bar effecting navigation bar in android

查看:154
本文介绍了将我的工具栏图像显示为状态栏的背景,从而影响android中的导航栏的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我希望我的工具栏占用状态栏的空间,并将用于其的图像作为状态栏的背景,并且我在活动中使用此代码成功地完成了此任务

I want my toolbar to take the space of status bar and the image used for it be the background of the status bar and I successfully do that with this code inside my activity

    //    for using status bar space
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_LAYOUT_NO_LIMITS,
                WindowManager.LayoutParams.FLAG_LAYOUT_NO_LIMITS);
    }

问题是此代码使我的活动布局甚至具有导航栏的全屏显示,因此该如何解决呢?

the problem is this code make my activity layout take the full screen even the space with navigation bar so how to solve this?

这是屏幕的图像

推荐答案

这是我用于在android 10和其他版本上测试的解决方案,因此请检查是否有帮助.

This is the solution I used for testing on android 10 and other versions, so check if this is of any help.

在您的styles.xml创建自定义主题中,如果将NoActionBar(AppCompat/DayNight/material等)中的任何一个设置为父级,或者直接设置一个(因为没有,则可以)将父主题设置为您的应用程序主题. NoActionBar会在顶部生成一个默认值),主要要求是以下三行:

In your styles.xml create a custom theme, you can set parent theme to what your app theme is if it has any of the NoActionBar (AppCompat/DayNight/material etc.) set as parent or set one directly(since without NoActionBar a default one will get generated on top), the major requirement is these three lines:

<style name="CustomTheme" parent="Theme.AppCompat.Light.NoActionBar">
    <item name="android:windowDrawsSystemBarBackgrounds">true</item>
    <item name="android:windowTranslucentStatus">false</item>
    <item name="android:statusBarColor">#00000000</item>
</style>

如果愿意,可以查看statusBarColor的文档,基本上可以告诉您需要做什么.

You can check the documentation for statusBarColor if you wish, basically tells you what needs to be done.

状态栏的颜色.如果颜色不是不透明,请考虑设置{@link android.view.View#SYSTEM_UI_FLAG_LAYOUT_STABLE}和{@link android.view.View#SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN}.为使此方法生效,该窗口必须使用{@link android.R.attr#windowDrawsSystemBarBackgrounds}绘制系统栏背景,并且必须不要求使用{@link android.R.attr#windowTranslucentStatus }.对应于{@link android.view.Window#setStatusBarColor(int)}.

The color for the status bar. If the color is not opaque, consider setting {@link android.view.View#SYSTEM_UI_FLAG_LAYOUT_STABLE} and {@link android.view.View#SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN}. For this to take effect, the window must be drawing the system bar backgrounds with {@link android.R.attr#windowDrawsSystemBarBackgrounds} and the status bar must not have been requested to be translucent with {@link android.R.attr#windowTranslucentStatus}. Corresponds to {@link android.view.Window#setStatusBarColor(int)}.

因此,只需根据需要将透明颜色或希望设置的任何颜色设置为状态栏.并将此主题设置为您想要的活动.

So simply set a transparent color or any color you wish to set to the status bar as per your requirement. And set this theme to the activity you want.

现在在活动中创建如下所示的方法:

Now in the activity create this method as shown below:

private void showCustomUI() {
        View decorView = getWindow().getDecorView();
        decorView.setSystemUiVisibility(
                View.SYSTEM_UI_FLAG_LAYOUT_STABLE
                        | View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN);
    }

这些是您需要的标志,设置标志时请勿添加View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION,因为它会隐藏您要显示的导航栏.

Those are the flags you require, don't add View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION while setting flags as it hides the navigation bar which your requirement is to show.

您需要调用该方法,否则状态栏将无法正确创建.

You'll need to call the method otherwise statusbar won't be created properly.

所以尝试一下,让我知道它是否对您有用.

So try this, and let me know if it worked for you.

如果您正在使用FLAG_LAYOUT_NO_LIMITS标志,请不要忘记删除它们,因为这会造成问题.这就是我对android 10进行测试的结果.

Don't forget to remove FLAG_LAYOUT_NO_LIMITS flags incase you have them in activity as it will create problems. Here's what i got on testing for android 10.

这篇关于将我的工具栏图像显示为状态栏的背景,从而影响android中的导航栏的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆