首先横竖屏切换不重建Activity的问题: 修改manifest文件将不但愿重建的Activity标签添加以下设置android
android:configChanges="orientation|keyboardHidden|screenSize"
复制代码
这样在屏幕旋转的时候Activity就不会重建了。bash
若是横竖屏布局相同的状况下,到此为止便可。app
固然,通常状况下横竖屏的布局确定要根据屏幕进行调整的。使用多布局文件来适配横屏和竖屏的方法,其余文章有介绍,这里就不记录了。ide
自从开始用了ConstraintLayout布局以后,就不多再用LinearLayout,RelativeLayout等旧的方法,实在是约束布局用起来太方便了,布局层级能够很是容易的保次比较低的状态,甚至不少时候就只有一层。布局
今天在作视频播放页面的时候要对横竖屏适配,其中有控制部分,须要动态设置控件间的依赖关系。 首先,既然要根据横竖屏来从新设置新的约束关系,就须要重写Activity的onConfigurationChanged方法,判断目标屏幕状态,并根据该状态对约束进行相应的调整测试
@Override
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
int mCurrentOrientation = getResources().getConfiguration().orientation;
//竖屏
if (mCurrentOrientation == Configuration.ORIENTATION_PORTRAIT) {
changeToPortraitLayout();
} else {
changeToLandscapeLayout();
}
}
复制代码
/**
* 动态设置竖屏状态下布局约束
*/
private void changeToPortraitLayout() {
ConstraintSet cs = new ConstraintSet();
//获取当前目标控件的约束集合
cs.clone(clRootContainer);
//修改surfaceView约束
//清除约束
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);
//修改控制切换按钮的约束,这里不能调用cs的clear方法清除约束,否者没法正常显示该控件
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);
//修改控制Fragment的约束
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);
//将修改过的约束从新应用到ConstrainLayout
cs.applyTo(clRootContainer);
}
复制代码
其中,必须先clone目标ConstraintLayout的约束,在现有约束上修改,否者新的约束会覆盖已有约束。 通过测试,若是其中一个方向上的约束没有显示设置,就不能调用clear方法清除旧的约束,我这里出现了控件没法显示的现象;ui
changeToLandscapeLayout方法和changeToPortraitLayout方法实现类似;spa
关于ConstraintSet的connect方法:code
connect方法有两个重载方法,分别有四个参数和五个参数(多一个margin参数);多的那个参数可让咱们设置该方向的margin;视频
另外四个参数以下: