public void onSensorChanged(SensorEvent e) {
if (e.sensor.getType()==Sensor.TYPE_ACCELEROMETER) {
if(e.values[0] >= 8)
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
else
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
}
}
当调用方法时setRequestedOrientation() - 错误。创建layout-land并在其中放置main.xml文件的副本。
答案 0 :(得分:0)
试试这个::
SensorManager sensorMgr;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
sensorMgr = (SensorManager) getSystemService(SENSOR_SERVICE);
sensorMgr.registerListener(this,
SensorManager.SENSOR_ACCELEROMETER,
SensorManager.SENSOR_DELAY_GAME);
}
public void onSensorChanged(int sensor, float[] values){
if (sensor==Sensor.TYPE_ACCELEROMETER) {
if(values[0] >= 8)
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
else
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
}
}