在 android 4.4+ 或 kitkat 中使用全屏隐藏状态栏 [英] Hide status bar in android 4.4+ or kitkat with Fullscreen
问题描述
我使用以下代码来隐藏全屏状态栏:
I'm using following code for hiding status bar with full screen purpose:
void HideEverything(){
if (Build.VERSION.SDK_INT < 16) {
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
} else {
View decorView = getWindow().getDecorView();
// Hide the status bar.
int uiOptions = View.SYSTEM_UI_FLAG_FULLSCREEN;
decorView.setSystemUiVisibility(uiOptions);
// Remember that you should never show the action bar if the
// status bar is hidden, so hide that too if necessary.
ActionBar actionBar = getActionBar();
actionBar.hide();
}
}
它以全屏模式显示屏幕,但当我触摸状态栏时,它显示状态栏.我尝试了其他选项在应用程序和清单中的活动级别中添加全屏和没有标题栏主题,但结果仍然相同...... :(
It shows Screen in full-screen mode but when I touch status bar it shows status bar. I tried other options to add the full screen and no title bar theme in application and activity level in manifest, but still results are the same... :(
我不想在任何步骤显示状态栏.如何在android中实现这一点?
I don't want to show status bar at any step. How to achieve this in android?
Android 操作系统版本为 4.4.2(以上代码适用于 4.3,但不适用于 4.4.2)
Android OS version is 4.4.2 (Above code is working on 4.3 but not on 4.4.2)
更新:
问题已解决...答案分别写在下面...
Problem has been solved... Answer is written below separately...
推荐答案
Android 版本为 4.4.2,无法解决此问题.
No solution could work for the problem as Android version is 4.4.2.
正如@Academy of Programmer 所评论的;此解决方案不适用于 Android 6 及更高版本.
但是按照Sunny
的建议,问题已经通过阻止状态栏的扩展解决了.
But as per the suggestion by Sunny
, problem has been solved by preventing the expansion of status bar.
解决方案是:
在您的主要活动中编写一个内联 customViewGroup
类.
Write an inline customViewGroup
class in your main activity.
public class customViewGroup extends ViewGroup {
public customViewGroup(Context context) {
super(context);
}
@Override
protected void onLayout(boolean changed, int l, int t, int r, int b) {
}
@Override
public boolean onInterceptTouchEvent(MotionEvent ev) {
Log.v("customViewGroup", "**********Intercepted");
return true;
}
}
现在在 mainActivity
的 onCreate
方法中,在 setContentView()
之前添加以下代码:
Now in your onCreate
method of mainActivity
add following code before setContentView()
:
WindowManager manager = ((WindowManager) getApplicationContext()
.getSystemService(Context.WINDOW_SERVICE));
WindowManager.LayoutParams localLayoutParams = new WindowManager.LayoutParams();
localLayoutParams.type = WindowManager.LayoutParams.TYPE_SYSTEM_ERROR;
localLayoutParams.gravity = Gravity.TOP;
localLayoutParams.flags = WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE|
// this is to enable the notification to receive touch events
WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL |
// Draws over status bar
WindowManager.LayoutParams.FLAG_LAYOUT_IN_SCREEN;
localLayoutParams.width = WindowManager.LayoutParams.MATCH_PARENT;
localLayoutParams.height = (int) (50 * getResources()
.getDisplayMetrics().scaledDensity);
localLayoutParams.format = PixelFormat.TRANSPARENT;
customViewGroup view = new customViewGroup(this);
manager.addView(view, localLayoutParams);
以上代码将禁用状态栏的扩展.现在通过以下代码全屏显示,将其添加到上述代码下方和 setContentView
之前:
Above code will disable the expansion of status bar.
Now make full screen by following code, add it just below the above code and before the setContentView
:
//fullscreen
requestWindowFeature(Window.FEATURE_NO_TITLE);
this.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,WindowManager.LayoutParams.FLAG_FULLSCREEN);
现在在清单中添加权限:
Now in manifest add the permission:
<!-- prevent expanding status bar -->
<uses-permission android:name="android.permission.SYSTEM_ALERT_WINDOW"/>
你就完成了....:)
and you are done.... :)
缺点:此代码禁用设备状态栏的扩展
,而不仅仅是针对活动或应用程序,因此如果您需要,请使用它真的很需要.
drawback : This code disables expansion of status bar for the device
not just for an activity or an application, so use it if you really need it.
感谢您提供的所有解决方案和建议... :)
这篇关于在 android 4.4+ 或 kitkat 中使用全屏隐藏状态栏的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!