orientation android

此生再无相见时 提交于 2019-12-13 22:19:40

问题


I want to add orientation on my app but I need that--> when my phone on PORTRAIT style works A activity and when I change PORTRAIT style as LANDSCAPE style A activity stops and B activity starts.How can I handle this? Thanks...


回答1:


if (getResources().getConfiguration().orientation == Configuration.ORIENTATION_PORTRAIT)
{   

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

} 



回答2:


do this

@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

    }
}



回答3:


Start your Activity B with below code -

@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
    }
  }

And, in your AndroidManifest.xml file's both activity tag. Just declare like below

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



回答4:


First check the orientation this way:

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

and start the Activity you wanted depending on the value of orientation.

then in the activity override this method:

public void onConfigurationChanged(Configuration newConfig) {

//start the other activity
}


来源:https://stackoverflow.com/questions/11828233/orientation-android

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!