displaying my toolbar image as background to status bar effecting navigation bar in android

你说的曾经没有我的故事 提交于 2020-08-08 05:37:21

问题


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?

here is an image for the screen


回答1:


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

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>

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

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

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.

EDIT: 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.




回答2:


Method 1

getWindow().setFlags(
    WindowManager.LayoutParams.FLAG_LAYOUT_NO_LIMITS,
    WindowManager.LayoutParams.FLAG_LAYOUT_NO_LIMITS
)

styles.xml

<style name="TranslucentStatusBar" parent="Theme.AppCompat.Light.NoActionBar">
    <item name="android:windowTranslucentStatus">true</item>
</style>

v21\styles.xml

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

status bar will be transparent or translucent, navigation bar won't

if setting windowTranslucentStatus to true won't work you can try this

Method 2

styles.xml ; just change the parent and don't add windowTranslucentStatus

<style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">

override style for v21 & v23

<style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
     <item name="android:windowDrawsSystemBarBackgrounds">false</item>
</style>

and add this in your activity.class

getWindow().setFlags(
        LayoutParams.FLAG_LAYOUT_NO_LIMITS,
        LayoutParams.FLAG_LAYOUT_NO_LIMITS
)

reference from answer1, answer2

hope this helps!




回答3:


By using these flags you can achieve that.

private void showSystemUI() {

    View decorView = getWindow().getDecorView();
    decorView.setSystemUiVisibility(
        View.SYSTEM_UI_FLAG_LAYOUT_STABLE
        | View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION
        | View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN);
}

call this method.



来源:https://stackoverflow.com/questions/60131341/displaying-my-toolbar-image-as-background-to-status-bar-effecting-navigation-bar

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!