用setRequestedOrientation
package com.example.androidtest;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.content.pm.ActivityInfo;
import android.content.res.Configuration;
import android.widget.CompoundButton;
import android.widget.ToggleButton;
import android.widget.CompoundButton.OnCheckedChangeListener;
public class AndroidTest extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_android_test);
ToggleButton toggle = (ToggleButton)findViewById(R.id.toggleButton);
if (getRequestedOrientation() != ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED) {
toggle.setChecked(true);
} else {
toggle.setChecked(false);
}
toggle.setOnCheckedChangeListener(listener);
}
OnCheckedChangeListener listener = new OnCheckedChangeListener()
{
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked)
{
int current = getResources().getConfiguration().orientation;
if (isChecked) {
switch(current) {
case Configuration.ORIENTATION_LANDSCAPE:
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
break;
case Configuration.ORIENTATION_PORTRAIT:
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
break;
default:
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED);
}
} else {
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED);
}
}
};
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.android_test, menu);
return true;
}
}