0

我想在我的应用程序上添加方向,但我需要它——>当我的手机在 PORTRAIT 样式上工作 A 活动时,当我将 PORTRAIT 样式更改为 LANDSCAPE 样式 A 活动停止并且 B 活动开始时。我该如何处理?谢谢...

4

4 回答 4

2

做这个

@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

    }
}
于 2012-08-06T12:28:28.673 回答
1
if (getResources().getConfiguration().orientation == Configuration.ORIENTATION_PORTRAIT)
{   

}
if (getResources().getConfiguration().orientation ==Configuration.ORIENTATION_LANDSCAPE)
{   

} 
于 2012-08-06T12:27:31.133 回答
1

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文件的两个活动标签中。只需像下面这样声明

<activity android:name=".A"
      android:configChanges="orientation|keyboardHidden"
      android:label="@string/app_name">
于 2012-08-06T12:30:15.127 回答
0

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

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

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

然后在活动中覆盖此方法:

public void onConfigurationChanged(Configuration newConfig) {

//start the other activity
}
于 2012-08-06T12:28:16.820 回答