This problem has bothered me for a long time and has not been properly resolved.
Scene 1: Huawei phone blocks the bottom of the screen.
Scene 2: When entering the application, the virtual key will automatically retract, leaving a blank area.
need:
Android needs to adapt to the bottom virtual key. When the user hides the virtual key, the application must fill the entire screen. When the user enables the virtual key, the application can shrink upward, which is equivalent to being pushed up by the bottom virtual key.
The requirements are simple, but they are difficult to implement.
Perfect solution:
Let me explain the following code, which is to monitor changes in a certain view. When the visible height changes, the view is re-layout to ensure that the view will not be blocked and screen space will not be wasted. This is especially useful on mobile phones like Huawei phones that can hide and display virtual keyboards that cause screen changes.
First add the tool class AndroidBug54971Workaround
package ; import ; import ; import ; import ; /** * Created by win7 on 2016/12/14. */ public class AndroidBug54971Workaround { // For more information, see /p/android/issues/detail?id=5497 // To use this class, simply invoke assistActivity() on an Activity that already has its content view set. /** * Associate the view to be listened to * * @param viewObserving */ public static void assistActivity(View viewObserving) { new AndroidBug54971Workaround(viewObserving); } private View mViewObserved;//The view being monitored private int usableHeightPrevious;//Available height before view changes private frameLayoutParams; private AndroidBug54971Workaround(View viewObserving) { mViewObserved = viewObserving; //Add a global layout listener to the View ().addOnGlobalLayoutListener(new () { public void onGlobalLayout() { resetLayoutByUsableHeight(computeUsableHeight()); } }); frameLayoutParams = (); } private void resetLayoutByUsableHeight(int usableHeightNow) { //Compare the available heights of the View before and after layout changes if (usableHeightNow != usableHeightPrevious) { //If the height is inconsistent between the two times //Set the available height of the current View to the actual height of the View = usableHeightNow; ();//Request to re-layout usableHeightPrevious = usableHeightNow; } } /** * Calculate the view visual height * * @return */ private int computeUsableHeight() { Rect r = new Rect(); (r); return ( - ); } }
Then add it after the setContentView(.content_frame); on the onCreate method of the Activity that you need to solve this problem
setContentView(.content_frame); (findViewById());
If you can understand the code, you must know that the View placed in the assistActivity method is the view you want to adjust the height.
Other imperfect solutions: more or less ineffective in some cases
My method:
android:fitsSystemWindows=”true”
This sentence is written in the root directory of layout. You can tell that it is an adaptive system window by looking at the name. It is estimated that it can solve a large number of mobile phones, but it is useless under my colleague's nexus 4.
The second method:
I removed android:fitsSystemWindows="true" for each layout
Added this sentence in the style file.
<item name="android:windowTranslucentNavigation">false</item>
Notice:You will find that the system reports an error, because this sentence is only available after API-19, so you can copy your style file and put it in the API-19 folder. This purpose is that if the phone is greater than or equal to API19, it will use the contents under the API-19 folder. Otherwise, use the original style file. It's OK to add the above sentence to the root theme of the style file in the API19 folder.
I thought it was the perfect solution to my problem. But I was slapped in the face. When you first enter the App, the above scenario 2 will appear.
I saw the setContentView() of the onCreate method in MainActivity; before the following code
//Control the bottom virtual keyboard getWindow().getDecorView().setSystemUiVisibility( 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 nav bar // | View.SYSTEM_UI_FLAG_FULLSCREEN // hide status bar | View.SYSTEM_UI_FLAG_IMMERSIVE);
It is probably added by the previous brother of this project to solve this problem.
After several debugging, I added a sentence
//Control the bottom virtual keyboard getWindow().getDecorView().setSystemUiVisibility( 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 nav bar // | View.SYSTEM_UI_FLAG_FULLSCREEN // hide status bar | View.SYSTEM_UI_FLAG_IMMERSIVE); getWindow().addFlags(.FLAG_TRANSLUCENT_NAVIGATION);
The situation in Scene 2 has been resolved. This is no problem when the virtual keys have always existed, because nexus cannot hide the virtual keyboard manually, so I don't know whether it can run normally on mobile phones such as Huawei. TODO.
In addition, if you want to hide the virtual keyboard all the time and click the screen will not appear, change the above code to:
//Make the virtual keyboard not display Window window = getWindow(); params = (); = View.SYSTEM_UI_FLAG_HIDE_NAVIGATION|View.SYSTEM_UI_FLAG_IMMERSIVE; (params);
The above article quickly solves the problem of virtual keyboards such as Android adaptation bottom return keys, which is all the content I have shared with you. I hope you can give you a reference and I hope you can support me more.