迁移到 api 21 工具栏后,我的全屏 activity 状态栏一半覆盖了我的工具栏

After migrating to api 21 toolbar my full screen activity status bar is halfway covering my toolbar

在我的应用程序中,我将这些标志用于全屏视频。在用户交互中,我显示 nav/status/tool 条。我正在使用带有工具栏的新 api 21 AppCompat。

这些是我在显示条形图时使用的标志:

int newVis = View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN | View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION | View.SYSTEM_UI_FLAG_LAYOUT_STABLE

然后调用:

mDecorView.setSystemUiVisibility( newVis );

它在迁移到 L 工具栏之前有效。大概现在工具栏被状态栏部分覆盖了,因为它是活动布局的一部分。我想我可以将工具栏向下移动一个等于状态栏高度的静态量,但这感觉很糟糕。有没有正确的方法来完成这个?

我的应用支持 android 4.1+。就像我说的,这在我开始使用工具栏之前并没有发生,但它确实发生在 android 4.1 - 5.0.

我通过添加框架布局(高度@dimen/statusbarheight)解决了这个问题。然后你可以设置每个版本和模式的高度。

一些示例:attrs.xml:

<dimen name="statusbarheight">0dp</dimen>

attrs.xml v21:

<dimen name="statusbarheight">25dp</dimen>

attrs.xml v21 土地:

<dimen name="statusbarheight">0dp</dimen>

当我在样式中设置半透明导航栏时,我遇到了同样的问题。

我想你在 KITKAT 上也会遇到类似的问题。参见DrawInsetsFrameLayout gist by Roman Nurik。布局应处理任何额外的填充并为其着色。

最后,修复涉及进行一些测量以确定操作栏和导航栏的大小。因为在横向 phone 大小的设备上,软导航栏最终位于显示屏的右侧,并且还覆盖了工具栏。下面的修复应该至少适用于 android api-16 到 api-21。代码的后半部分只是为了重置工具栏的外观,由于某种原因,操作栏中的代码图标最终没有正确对齐(它们更靠近顶部)。希望这对其他人有帮助。

// Get the root activity layout id
            RelativeLayout root = (RelativeLayout) findViewById( R.id.container );
            root.post( new Runnable()
            {
                public void run()
                {
                    Point size = new Point();
                    Display display = getWindowManager().getDefaultDisplay();
                    if ( Integer.valueOf( android.os.Build.VERSION.SDK ) >= Build.VERSION_CODES.JELLY_BEAN_MR1 )
                    {
                        // This is only available on api 17 and higher
                        display.getRealSize( size );
                    }
                    else
                    {
                        // On api 16 and less this should work
                        display.getSize( size );
                    }

                    int screenWidth = size.x;

                    Rect rect = new Rect();
                    Window win = getWindow();  // Get the Window
                    win.getDecorView().getWindowVisibleDisplayFrame( rect );
                    // Get the height of Status Bar 
                    int statusBarHeight = rect.top;
                    int navBarWidth = screenWidth - rect.width();

                    RelativeLayout.LayoutParams params = (RelativeLayout.LayoutParams) mToolbar.getLayoutParams();

                    android.util.TypedValue value = new android.util.TypedValue();
                    getTheme().resolveAttribute( android.R.attr.actionBarSize, value, true );
                    TypedValue.coerceToString( value.type, value.data );
                    android.util.DisplayMetrics metrics = new android.util.DisplayMetrics();
                    getWindowManager().getDefaultDisplay().getMetrics( metrics );
                    params.height = (int) value.getDimension( metrics );

                    params.setMargins( 0, statusBarHeight, navBarWidth, 0 );
                    mToolbar.setLayoutParams( params );
                }
            } );

此外 root.post(Runnable ...) 部分是因为我们无法测量 window 直到 oncreate 完成。