To meet this demand, first look at the picture:
In fact, is a software login interface, the initial is the first figure, when the soft keyboard pop-up into the second figure, because the login interface has a user name, password, login button, not so the soft keyboard pop-up will cover the login button (in fact, before the implementation of the ScrollView inside, Listen to the soft keyboard popup after scrolling to the bottom, soft keyboard hidden after scrolling to the top, is also possible).
The simplest way is to add a few redundant view, hide the unwanted view according to the state of the soft keyboard, display the desired view, but this feeling is too bad, and then think of the previous two years of research relativelayout layout, Relativelayout the layout of the neutron control is relative, just need to change the location rules of the app when the soft keyboard pops up hidden.
Let's take a look at the layout file
<relativelayout xmlns:android= "http://schemas.android.com/apk/res/android" xmlns:tools= "http// Schemas.android.com/tools "android:id=" @+id/root "android:layout_width=" match_parent "android:layout_height=" MATC H_parent "android:padding=" 20DP "tools:context=" ${packagename}.${activityclass} "> <relativelayout a Ndroid:id= "@+id/container" android:layout_width= "match_parent" android:layout_height= "Wrap_content" a Ndroid:layout_alignparenttop= "true" > <imageview android:id= "@+id/logo" Android:layout_ Width= "150DP" android:layout_height= "150DP" android:layout_centerhorizontal= "true" Androi D:scaletype= "Centercrop" android:src= "@drawable/ic_launcher" tools:ignore= "ContentDescription"/> <textview android:id= "@+id/label" android:layout_width= "Wrap_content" Android: layout_height= "Wrap_content" Android:layout_below= "@id/logo" android:layout_centerhorizontal= "true" android:layout_marginleft= "10DP" android:layout_margintop= "10DP" android:text= "@string/hello_world" android:textsize= "20SP" /> </RelativeLayout> <edittext android:id= "@+id/input" android:layout_width= "match_parent "Android:layout_height=" wrap_content "android:layout_below=" @id/container "android:layout_margin=" 16 DP "android:hint=" Input sth. " Tools:ignore= "Hardcodedtext"/></relativelayout>
Soft keyboard pop-up hidden with ongloballayoutlistener monitoring implementation, the activity application android:windowsoftinputmode= "Statehidden|adjustresize", This starts when the soft keyboard is not displayed and the layout is resize when the soft keyboard pops up.
And then there's the code, and all the code is here.
public class Mainactivity extends Activity {private view root;//outermost layout private view logo;//logo icon Private Vie W label; The text near the logo private int rootbottom = Integer.min_value; @Override protected void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate); Setcontentview (R.layout.activity_main); Root = Findviewbyid (r.id.root); Logo = Findviewbyid (R.id.logo); Label = Findviewbyid (R.id.label); Root.getviewtreeobserver (). Addongloballayoutlistener (New Ongloballayoutlistener () {@Override public void Ongloballayout () {rect r = new Rect (); Root.getglobalvisiblerect (R); Enter activity will layout, first call Ongloballayout, first record start soft keyboard does not eject when the bottom position if (Rootbottom = = Integer.min_value) { Rootbottom = R.bottom; Return }//Adjustresize, after the soft keyboard pops up the height will become smaller if (R.bottom < RootbottOM) {relativelayout.layoutparams LP = (layoutparams) logo.getlayoutparams (); If the logo is not centered horizontally, it is because the next change in the logo size position caused by the layout again, ignored, otherwise infinite loop if (Lp.getrules () [Relativelayout.center_horizonta L]! = 0) {//logo display to upper left corner lp.addrule (relativelayout.center_horizontal, 0);// Cancels the horizontal center lp.addrule (relativelayout.align_parent_left); Left alignment//Zoom out logo for the logo.getheight int height = n (); Getmeasuredheight () int width = Logo.getwidth (); Lp.width = WIDTH/2; Lp.height = HEIGHT/2; LOGO.SETLAYOUTPARAMS (LP); The text under the logo relativelayout.layoutparams labelparams = (layoutparams) label.getlayoutparams (); Labelparams.addrule (relativelayout.center_horizontal, 0); Cancel Horizontal Centering Labelparams.addrule (relativelayout.below, 0); Cancel the display to the logo below Labelparams.addrule (relativelayout.right_of, R.id.logo); Display to the right of the logo labelparams.addrule (relativelayout.center_vertical); Vertical Center Label.setlayoutparams (labelparams); }} else {//relativelayout.layoutparams LP = (layoutparams) When the soft keyboard is closed or initialized LOGO.GETLAYOUTP Arams (); If there is no horizontal center, the description is the soft keyboard is closed, otherwise it is the beginning of the initialization or because the statement in the IF condition to modify the control caused by the layout again, ignore, otherwise infinite loop if (Lp.getrules () [Relativelayout.ce Nter_horizontal] = = 0) {//Center logo lp.addrule (relativelayout.center_horizont AL); Lp.addrule (relativelayout.align_parent_left, 0); Restore logo for original size int height = logo.getheight (); int width = logo.getwidth (); Lp.width = width * 2; Lp.height = height * 2; LOGO.SETLAYOUTPARAMS (LP); The text under the logo relativelayout.layoutparams labelparams = (layoutparams) label.getlayoutparams (); Labelparams.addrule (relativelayout.center_horizontal); Set the horizontal center Labelparams.addrule (relativelayout.below, R.id.logo); The settings are displayed below the logo labelparams.addrule (relativelayout.right_of, 0); Cancel display to the right of logo labelparams.addrule (relativelayout.center_vertical, 0); Cancels the vertical center label.setlayoutparams (labelparams); } } } }); }}
When the activity is started, it will also be layout, when the initial outermost layout is recorded at the bottom of the position, and then when the soft keyboard pops up, the layout is compressed, again get the same view at the bottom of the position, if the rootbottom than Rootbottom Small description soft keyboard pops up, if greater than or equal to rootbottom description soft keyboard hidden.
All the code is on the top, there are detailed comments, there are two points to note:
- When the activity is started, the layout will be called Ongloballayout, and will generally call two times, so the second time will enter the Else statement, pay attention to filter
- When the soft keyboard pops up or hides into the ongloballayout, the size of the logo can be scaled as needed, and the location of the logo and label is changed, and these actions will cause the ongloballayout to be re- Ongloballayout filter out, or the infinite cycle.
You can see the filter in the above code, in the Else statement as an example, when the activity starts will go to else, when the logo is horizontally centered state, will skip the else inside if statement, so that the first case is disposed of.
When the soft keyboard is put into the else, the logo has been because the IF statement block to display in the upper left corner, so will go into the else if statement, re-change the logo to the horizontal center, due to change the size and position of the logo, will lead to re-entry ongloballayout , still go to else, but at this point the logo has been set to the horizontal center, will not go into the else if statement, so through a condition to judge the above mentioned two points of attention.
About Addrule
The rules that are applied to each child control in Relativelayout are saved by an array, as follows:
public static final int TRUE = -1;public void addRule (int verb) { Mrules[verb] = true; Minitialrules[verb] = TRUE; Mruleschanged = true;} public void addRule (int verb, int anchor) { Mrules[verb] = anchor; Minitialrules[verb] = anchor; Mruleschanged = true;}
The index of a rule is subscript, the value is the anchor of the rule, if it is relative to another child control, the value is the ID of another child control, if it is relative to the parent control, the value is ' TRUE ', that is-1, if a rule value is not applied is 0, you can see that Removerule is to change the value of the corresponding position to 0:
public void removerule (int verb) { Mrules[verb] = 0; Minitialrules[verb] = 0; Mruleschanged = true; }
removerule is the API to add the method, in order to be able to use before the API , you can use its equivalent method, as in the example above, using the addRule (verb , 0).
Android dynamic change layout, such as landing pop-up soft keyboard, landing box up (reprint)