横竖屏切换
- activity 会被销毁
- 通过修改配置实现,横竖屏切换不销毁 android:configChanges="orientation|keyboardHidden|screenSize"
- 写死横竖屏
- 横屏模式 android:screenOrientation="landscape"
- 竖屏模式 android:screenOrientation="portrait"
- 还可以通过代码来实现,且代码实现的优先级高于xml中配置的,采用就近原则
<activity
android:screenOrientation="landscape"
android:configChanges="orientation|keyboardHidden|screenSize"
android:name=".MainActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT); //设置为竖屏
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE); //设置为横屏
System.out.println("主 onCreate 调用");
}
网友评论