繁体   English   中英

Android如何测试方向变化

[英]Android how to test orientation change

我有一个关于单元测试android方向转换的问题。 我在我的应用程序中支持Portrait和Landscape,我必须测试在方向更改时是否正确绘制了视图层次结构。

我创建了两个测试方法来检查这个,我有这样的事情:

public void testOnCreate() throws Exception {
    //Check all the activity components
    assertNotNull(activity);
    assertNotNull(application);

    //Check if the rights components are available on the screen
    assertNotNull(LayoutInflater.from(activity));
    activity.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
    testOrientationPortrait();
}

在这种特殊情况下,测试通过,并正确绘制视图层次结构。 但是当我尝试使用以下方法测试景观时:

public void testOrientationChange() throws Exception {
    assertNotNull(activity);
    activity.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);

    //Check if the rights components are available on the screen
    assertNotNull(LayoutInflater.from(activity));
    testOrientationLandscape();
}

方向更改,但视图层次结构失败,因为视图具有纵向属性。

任何想法如何解决这一问题?

谢谢,方舟

重写此方法并在方法中进行更改:

@Override
 public void onConfigurationChanged(Configuration newConfig) {
  // TODO Auto-generated method stub
  super.onConfigurationChanged(newConfig);


 }

别忘了添加

 <activity

        android:configChanges="orientation"
        >
    </activity>

在你的清单中。

这可以用来检查方向。 getResources()。getConfiguration()。方向

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM