First, the problem with switching between horizontal and vertical screens without rebuilding an Activity: Modify the manifest file to add the following Settings to the Activity tag that you do not want to rebuild
android:configChanges="orientation|keyboardHidden|screenSize"
Copy the code
This way the Activity does not rebuild while the screen rotates.
If the horizontal and vertical layouts are the same, stop here.
Of course, in general, the horizontal and vertical screen layout must be adjusted according to the screen. The method of using multiple layout files to adapt landscape and portrait is described in other articles but not documented here.
ConstraintLayout, LinearLayout, RelativeLayout and so on are rarely used again. It is so convenient to use the layout hierarchy that it is very easy to maintain a low state, or even a lot of times there is only one layer.
Today when doing video playback page to vertical screen adaptation, which has control part, need to dynamically set the dependency between controls. First, since you want to reset the new constraint based on vertical and horizontal screens, you need to rewrite the Activity’s onConfigurationChanged method to determine the target screen state and adjust the constraint based on that state
@Override public void onConfigurationChanged(Configuration newConfig) { super.onConfigurationChanged(newConfig); int mCurrentOrientation = getResources().getConfiguration().orientation; / / vertical screenif (mCurrentOrientation == Configuration.ORIENTATION_PORTRAIT) {
changeToPortraitLayout();
} else{ changeToLandscapeLayout(); }}Copy the code
/** * Private void */ private voidchangeToPortraitLayout() { ConstraintSet cs = new ConstraintSet(); // Get the constraint set cs.clone(clRootContainer) for the current target control; Cs.clear (surfaceView.getid ()); cs.connect(surfaceView.getId(), ConstraintSet.LEFT, clRootContainer.getId(), ConstraintSet.LEFT); cs.connect(surfaceView.getId(), ConstraintSet.TOP, findViewById(R.id.topTitle).getId(), ConstraintSet.BOTTOM); cs.connect(surfaceView.getId(), ConstraintSet.RIGHT, clRootContainer.getId(), ConstraintSet.RIGHT); cs.connect(surfaceView.getId(), ConstraintSet.BOTTOM, findViewById(R.id.guideCenter).getId(), ConstraintSet.TOP); // Change the control switch button constraint, here can not call cs clear method to clear the constraint, Cs.connect (rGrobotControl.getid (), constraintset. LEFT, clRootContainer.getid (), constraintSet.left); cs.connect(rgRobotControl.getId(), ConstraintSet.TOP, findViewById(R.id.guideCenter).getId(), ConstraintSet.BOTTOM); cs.connect(rgRobotControl.getId(), ConstraintSet.RIGHT, clRootContainer.getId(), ConstraintSet.RIGHT); Clear (controlContainer.getid ()); // Modify the Fragment control constraint cs.clear(controlContainer.getid ()); cs.connect(controlContainer.getId(), ConstraintSet.TOP, rgRobotControl.getId(), ConstraintSet.BOTTOM); cs.connect(controlContainer.getId(), ConstraintSet.LEFT, rgRobotControl.getId(), ConstraintSet.LEFT); cs.connect(controlContainer.getId(), ConstraintSet.RIGHT, rgRobotControl.getId(), ConstraintSet.RIGHT); cs.connect(controlContainer.getId(), ConstraintSet.BOTTOM, clRootContainer.getId(), ConstraintSet.BOTTOM); // Reapply the modified constraint to ConstrainLayout cs.applyto (clRootContainer); }Copy the code
Clone the target ConstraintLayout constraint and modify it on the existing constraint. Otherwise, the new constraint overwrites the existing constraint. After testing, if the constraint in one direction is not displayed, the clear method cannot be called to clear the old constraint.
The changeToLandscapeLayout method is implemented similarly to the changeToPortraitLayout method.
The connect method for ConstraintSet:
The connect method has two overloaded methods, with four parameters and five parameters respectively (one margin parameter). The extra parameter allows us to set the margin in that direction;
The other four parameters are as follows:
- StartID: ID of the control on which the constraint is to be set, obtained from the View#getId() control method.
- StartSide: Which direction constraint to set, corresponding to the XML filelayout_constraintLeft_toRightOfAttributes like underline to the leftLeftPart;
- EndID: constraint reference control ID, which control to set the constraint, corresponding to the XML file layout_constraintLeft_toLeftOf=”parent” attribute parameters;
- EndSide: Sets the direction of the constraint provided by the reference control: corresponding to the XML filelayout_constraintLeft_toRightOfAttributes to the right of the underlineRightPart;