迁移到api 21工具栏后,我的全屏活动状态栏已经覆盖了我的工具栏
问题描述:
在我的应用程序中,我将这些标志用于全屏视频。在用户交互中,我显示导航/状态/工具栏。我正在使用带工具栏的新API 21 AppCompat。迁移到api 21工具栏后,我的全屏活动状态栏已经覆盖了我的工具栏
这些都是显示栏时,我使用的标志:
mDecorView.setSystemUiVisibility(newVis);
它迁移到L工具栏前的工作:
int newVis = View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN | View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION | View.SYSTEM_UI_FLAG_LAYOUT_STABLE
然后调用。据推测,现在工具栏部分被状态栏覆盖,因为它是活动布局的一部分。我想我可以将工具栏向下移动一个等于状态栏高度的静态数量,但感觉这是一种黑客行为。有没有一个正确的方法来完成这个?
我的应用程序支持android 4.1+。就像我说的,在我开始使用工具栏之前,这并没有发生,但它确实发生在android 4.1 - 5.0上。
答
最后,修复包括做一些测量来确定操作栏和导航栏的大小。由于在横向手机尺寸的设备上,软导航栏最终显示在显示器的右侧,并且还遮盖了工具栏。下面的修复应该至少在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 ...)部分是因为我们无法测量窗口,直到oncreate完成。
答
我解决了它添加一个framelayout(height @ 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>
当我里面设置半透明导航栏风格我得到了同样的问题。
这不起作用。 25dp不够高。另外我不确定所有设备的高度是否相同。或者是? – startoftext 2015-01-27 00:45:09
标准高度是24dp,但我担心这可以通过一些主题应用程序进行更改。 – 2015-01-27 01:36:32
我试了nexus 5中的应用程序,nexus one和25 dp修复了我的问题。像谷歌衡量指标24 dp说对我来说是错误的。 – Dahnark 2015-01-27 01:39:00