Android 横竖屏切换,约束布局动态修改约束

4,639 阅读2分钟

首先横竖屏切换不重建Activity的问题: 修改manifest文件将不希望重建的Activity标签添加如下设置

android:configChanges="orientation|keyboardHidden|screenSize"

这样在屏幕旋转的时候Activity就不会重建了。

如果横竖屏布局相同的情况下,到此为止即可。


当然,一般情况下横竖屏的布局肯定要根据屏幕进行调整的。使用多布局文件来适配横屏和竖屏的方法,其他文章有介绍,这里就不记录了。

自从开始用了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方法清除旧的约束,我这里出现了控件无法显示的现象;

changeToLandscapeLayout方法和changeToPortraitLayout方法实现相似;

关于ConstraintSet的connect方法:

connect方法有两个重载方法,分别有四个参数和五个参数(多一个margin参数);多的那个参数可以让我们设置该方向的margin;

另外四个参数如下:

  • startID:需要设置约束的控件的ID,通过控件View#getId()方法获取;
  • startSide:要设置哪个方向的约束,对应xml文件中layout_constraintLeft_toRightOf等属性下划线左边的Left部分;
  • endID:约束参考控件的ID,设置与哪一个控件的约束,对应xml文件中layout_constraintLeft_toLeftOf="parent"等属性的参数;
  • endSide:设置参考控件提供的约束的方向:对应xml文件中layout_constraintLeft_toRightOf等属性下划线右边的Right部分;