问题
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