orientation android



我想在我的应用程序上添加方向,但我需要-->当我的手机在PORTRAIT风格上运行A活动时,以及当我将PORTRAIT样式更改为景观风格时,A活动停止,B活动开始。我该如何处理?谢谢

执行此

@Override
public void onConfigurationChanged(Configuration newConfig) {
    super.onConfigurationChanged(newConfig);
    // Checks the orientation of the screen
    if (newConfig.orientation == Configuration.ORIENTATION_LANDSCAPE) {
        Toast.makeText(this, "landscape", Toast.LENGTH_SHORT).show();
       //here call activity A
    } else if (newConfig.orientation == Configuration.ORIENTATION_PORTRAIT){
        Toast.makeText(this, "portrait", Toast.LENGTH_SHORT).show();
       //here call activity B
    }
}
if (getResources().getConfiguration().orientation == Configuration.ORIENTATION_PORTRAIT)
{   
}
if (getResources().getConfiguration().orientation ==Configuration.ORIENTATION_LANDSCAPE)
{   
} 

使用以下代码启动Activity B-

@Override
public void onConfigurationChanged(Configuration newConfig) {
    super.onConfigurationChanged(newConfig);
    // Checks the orientation of the screen
    if (newConfig.orientation == Configuration.ORIENTATION_LANDSCAPE) {
        Intent a = new Intent(this, B.class);
        startActivity(a);
    } else if (newConfig.orientation == Configuration.ORIENTATION_PORTRAIT){
        //do nothing
    }
  }

并且,在AndroidManifest.xml文件的both activity标记中。只需像下面的一样申报即可

<activity android:name=".A"
      android:configChanges="orientation|keyboardHidden"
      android:label="@string/app_name">

首先以这种方式检查方向:

Display display = ((WindowManager) getSystemService(WINDOW_SERVICE)).getDefaultDisplay();
int orientation = display.getOrientation();

并根据方向值启动您想要的"活动"。

则在活动中覆盖此方法:

public void onConfigurationChanged(Configuration newConfig) {
//start the other activity
}

最新更新