在移动应用开发中,有时候我们需要让应用全屏显示,以提供更好的用户体验。
分享一个使用Autojs实现全屏功能的代码,来源于@つ゛ Shrek・流云 ଲ͏
效果
代码
'ui';
// 设置全屏
function FullScreen() {
// 导入一些所需类
importClass(android.os.Build);
importClass(android.view.View);
const LayoutParams = android.view.WindowManager.LayoutParams;
if (Build.VERSION.SDK_INT >= 29) {
activity.getWindow().getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_HIDE_NAVIGATION);
activity.getWindow().getAttributes().layoutInDisplayCutoutMode = 1;
activity.getWindow().addFlags(LayoutParams.FLAG_TRANSLUCENT_STATUS | LayoutParams.FLAG_TRANSLUCENT_NAVIGATION | LayoutParams.FLAG_FULLSCREEN);
} else {
activity
.getWindow()
.getDecorView()
.setSystemUiVisibility(View.SYSTEM_UI_FLAG_HIDE_NAVIGATION | View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY | View.SYSTEM_UI_FLAG_FULLSCREEN);
activity.getWindow().addFlags(LayoutParams.FLAG_LAYOUT_NO_LIMITS);
}
}
// 关闭全屏
function exitFullScreen() {
importClass(android.os.Build);
importClass(android.view.View);
const LayoutParams = android.view.WindowManager.LayoutParams;
const WindowManager = android.view.WindowManager;
// 清除之前设置的标志和视图属性
if (Build.VERSION.SDK_INT >= 29) {
// 清除全屏标志和透明状态栏、导航栏标志
activity.getWindow().clearFlags(LayoutParams.FLAG_TRANSLUCENT_STATUS | LayoutParams.FLAG_TRANSLUCENT_NAVIGATION | LayoutParams.FLAG_FULLSCREEN);
activity.getWindow().getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_VISIBLE);
activity.getWindow().getAttributes().layoutInDisplayCutoutMode = WindowManager.LayoutParams.LAYOUT_IN_DISPLAY_CUTOUT_MODE_DEFAULT;
} else {
// 清除全屏标志和沉浸式、全屏标志
activity.getWindow().clearFlags(LayoutParams.FLAG_LAYOUT_NO_LIMITS);
activity.getWindow().getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_VISIBLE);
}
}
ui.layout(
<vertical w="*" h="*">
<button id="actionSet" text="设置全屏" />
<button id="actionOff" text="关闭全屏" />
</vertical>
);
ui.actionSet.click(() => {
FullScreen();
});
ui.actionOff.click(() => {
exitFullScreen();
});