始终禁用自定义启动器中的状态栏和底部栏
我只需要编写一个带有我的应用程序并禁用android系统状态栏和底部栏的自定义android启动器。这是我的代码
I need to write custom android launcher with only one my application and with disabling android system statusbar and bottom bar. Here is my code
在 AndroidManifest.xml
<activity android:name=".MainActivity"
android:launchMode="singleTask"
android:stateNotNeeded="true">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER"/>
<category android:name="android.intent.category.HOME" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
在 styles.xml
<item name="android:windowShowWallpaper">true</item>
<item name="android:windowBackground">@android:color/transparent</item>
ins MainActivity.class
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
hideSystemUi()
buttonStartMyApp.setOnClickListener {
Toast.makeText(this@MainActivity, "Start own app", Toast.LENGTH_SHORT).show()
}
}
private fun hideSystemUi() {
window.decorView.systemUiVisibility = (View.SYSTEM_UI_FLAG_LAYOUT_STABLE
or View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION
or View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
or View.SYSTEM_UI_FLAG_HIDE_NAVIGATION
or View.SYSTEM_UI_FLAG_FULLSCREEN
or View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY)
window.addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION)
window.addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS)
}
此启动器仅用一个按钮启动(没关系),并且没有状态栏和底部导航栏。但是我们可以通过在屏幕顶部自上而下的滑动来获取它们。如何禁用此功能?谢谢。
This launcher starts with only one button (its okay) and without status bar and bottom nav bar. BUT we can to get them with top-down swipe on the top of the screen. How to disable this function? Thanks.
您已实现了 IMMERSIVE_STICKY
标志,该标志允许状态屏幕顶部的上下滑动即可调用该条。要禁用此功能:
You have implemented the IMMERSIVE_STICKY
flag which allows the status bar to be called by top-down swipe on the top of the screen. To disable this:
super.onCreate(savedInstanceState);
getWindow().addFlags(WindowManager.LayoutParams.TYPE_SYSTEM_OVERLAY);
setContentView(R.layout.your_layout);
要禁用状态下拉菜单,请使用此方法。这是状态栏的覆盖,并消耗了所有输入事件。
To disable status drop down, use this method. This is an overlay over status bar and consumed all input events. It prevents the status from expanding.
注意:
customViewGroup
是自定义类,可扩展任何布局(框架,相对布局等)并使用touch事件。
要使用触摸事件,请重写视图组的onInterceptTouchEvent方法并返回true。
customViewGroup
is custom class which extends any layout(frame,relative layout etc) and consumes touch event.
To consume touch event override the onInterceptTouchEvent method of the view group and return true.
Android Manifest
<uses-permission android:name="android.permission.SYSTEM_ALERT_WINDOW"/>
customViewGroup实施
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 recieve 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);