Detailed Explanation of the Method of Android Adapting to the Bottom Virtual Key
- 2021-09-20 21:38:42
- OfStack
When the project was adapted recently, it was found that some mobile phones (such as Huawei mobile phones) with virtual keys at the bottom would block some interfaces because of the existence of virtual keys. Because full-screen display was needed, the virtual key hiding method was called to hide them. However, the following problems were found:
Long white strip area appears after manually operating the hidden virtual key
Do not hide automatically
After slipping out of the status bar, the virtual buttons also come out, and the status bar is hidden but not hidden
On the device without virtual buttons, it affects the full-screen display of SurfaceView (the original full-screen display becomes a small-screen display when it is cut out and then comes in)
Through google, many methods and attempts have finally solved this problem, achieving the following results:
The virtual keys are automatically hidden every time they enter the interface
Slide out the virtual key manually, and automatically hide it when the screen is not operated
When sliding out of the status bar, the virtual keys will follow. Also, it should be handled so that it and the status bar will be automatically hidden when not operating
The specific code is as follows:
public class TestActivity extends AppCompatActivity {
private View decorView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_test);
// Get top-level view
decorView = getWindow().getDecorView();
}
@Override
protected void onStart() {
// Call configuration
init();
super.onStart();
}
private void init(){
int flag = View.SYSTEM_UI_FLAG_LAYOUT_STABLE
| View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION
| View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
| View.SYSTEM_UI_FLAG_HIDE_NAVIGATION // hide
| View.SYSTEM_UI_FLAG_FULLSCREEN // hide status bar
| View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY;
// Determine that the current version is in 4.0 There are virtual keys above, otherwise no operation will be done
if (Build.VERSION.SDK_INT < 19 || !checkDeviceHasNavigationBar()) {
//1 Be sure to judge whether there are buttons, otherwise calling on a mobile phone without buttons will affect other functions. If not considered before, the full screen of picture transmission will become a small screen display.
return;
} else {
// Get properties
decorView.setSystemUiVisibility(flag);
}
}
/**
* Determining whether there is a virtual key
* @return
*/
public boolean checkDeviceHasNavigationBar() {
boolean hasNavigationBar = false;
Resources rs = getResources();
int id = rs.getIdentifier("config_showNavigationBar", "bool", "android");
if (id > 0) {
hasNavigationBar = rs.getBoolean(id);
}
try {
Class<?> systemPropertiesClass = Class.forName("android.os.SystemProperties");
Method m = systemPropertiesClass.getMethod("get", String.class);
String navBarOverride = (String) m.invoke(systemPropertiesClass, "qemu.hw.mainkeys");
if ("1".equals(navBarOverride)) {
hasNavigationBar = false;
} else if ("0".equals(navBarOverride)) {
hasNavigationBar = true;
}
} catch (Exception e) {
}
return hasNavigationBar;
}
@Override
public boolean onTouch(View v, MotionEvent event) {
return false;
}
Note: Sometimes you have to manually call the init () configuration method in the above code to hide the keys. For example, when dialog pops up, the virtual keys will come out. At this time, it is necessary to manually call hiding, and pop up the keyboard and so on.
Record 1, which can be used for reference when encountering such problems in the future.