我正在使用显示在锁定屏幕顶部的 web 视图,而且我会在短时间内自动隐藏系统栏:
/**
* Class used to hide the device System UI bar.
*
*/
public class SystemUiHider {
/**
* Delay until to hide the bar.
*/
private static final int HIDE_DELAY_MILLIS = 3000;
/**
* Declare the handler.
*/
private Handler mHandler;
/**
* Declare the view on which to hide the bar.
*/
private View mView;
private Utils utils;
/**
* Set the class view to be used.
*
* @param view
*/
public SystemUiHider(final View view, final Context context) {
this.mView = view;
this.utils = new Utils(context);
}
/**
* Setup used to hide the system bar and start the handler.
*/
public final void setup() {
hideSystemUi();
this.mHandler = new Handler();
mView.setOnSystemUiVisibilityChangeListener(new View.OnSystemUiVisibilityChangeListener() {
@Override
public void onSystemUiVisibilityChange(final int visibility) {
if (visibility != View.SYSTEM_UI_FLAG_HIDE_NAVIGATION) {
delay();
}
}
});
}
/**
* Set the flag to hidden for the view.
*/
private void hideSystemUi() {
utils.hideSystemBar(WebViewActivity.appContext);
}
/**
* Set the runnable that runs on a different thread to call the method
* hiding the system bar.
*/
private Runnable mHideRunnable = new Runnable() {
public void run() {
hideSystemUi();
}
};
/**
* Set the delay until to hide the system bar.
*/
private void delay() {
mHandler.removeCallbacks(mHideRunnable);
mHandler.postDelayed(mHideRunnable, HIDE_DELAY_MILLIS);
}
}
这是隐藏系统栏的方法:
/**
* Hide system bar.
* Different methods applied depending on the SDK version
*
* @param context
*/
@SuppressLint("InlinedApi")
@SuppressWarnings("deprecation")
public void hideSystemBar(Context context) {
if (SDK_INT < FOURTEEN) {
Log.i("WebViewClient", "hideSystemBar: < 14 ");
((Activity) context).getWindow().getDecorView().setSystemUiVisibility(View.STATUS_BAR_HIDDEN);
} else if (SDK_INT >= FOURTEEN && SDK_INT < SIXTEEN) {
Log.i("WebViewClient", "hideSystemBar: 14 - 16");
((Activity) context).getWindow().getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LOW_PROFILE // 14
| View.SYSTEM_UI_FLAG_HIDE_NAVIGATION); // 14
} else if (SDK_INT >= SIXTEEN) {
Log.i("WebViewClient", "hideSystemBar: > 16");
((Activity) context).getWindow().getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LOW_PROFILE // 14
| View.SYSTEM_UI_FLAG_HIDE_NAVIGATION // 14
| View.SYSTEM_UI_FLAG_FULLSCREEN); // 16
}
}
我遇到的问题是我的 webview 在使用它时失去了焦点。我的猜测是因为((Activity) context).getWindow().getDecorView()
是否有另一种方法可以在锁定屏幕顶部显示活动并隐藏系统栏而不失去焦点?
谢谢你。