方法1:禁止旋转屏幕- <activity android:name=".MyActivity"
- android:screenOrientation="portrait"
- android:label="@string/app_name">
复制代码
方法2:旋转后恢复现场
- @Overridepublic void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState);
- setContentView(R.layout.main); final MyDataObject data = (MyDataObject) getLastNonConfigurationInstance(); if (data == null) {//表示不是由于Configuration改变触发的onCreate()
- data = loadMyData();
- }
- ...
- }
复制代码
方法3:手工处理旋转
- @Overridepublic void onConfigurationChanged(Configuration newConfig) { super.onConfigurationChanged(newConfig);
- // Checks the orientation of the screen
- if (newConfig.orientation == Configuration.ORIENTATION_LANDSCAPE) {
- Toast.makeText(this, "横屏模式", Toast.LENGTH_SHORT).show();
- } else if (newConfig.orientation == Configuration.ORIENTATION_PORTRAIT){
- Toast.makeText(this, "竖屏模式", Toast.LENGTH_SHORT).show();
- }
- }
复制代码 |
|