在您的清单文件中,为您的相机预览活动提供以下属性以
android:configChanges="keyboardHidden|orientation"
获取更多信息
<activity
android:name=".Your_Activity_Name"
android:configChanges="keyboardHidden|orientation"
android:label="@string/app_name" >
<intent-filter>
<!-- Your filter attributes if any. -->
</intent-filter>
</activity>
并在您的活动类中覆盖onConfigurationChanged
并在关注屏幕方向模式(例如,横向或纵向)中执行必要的(根据您的需要),如下所示_
@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();
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
//Your code to do the other things in landscape mode...
} else if (newConfig.orientation == Configuration.ORIENTATION_PORTRAIT){
Toast.makeText(this, "portrait", Toast.LENGTH_SHORT).show();
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
//Your code to do the other things in portrait mode...
}
}
我希望这能帮助你摆脱你的问题。