我刚刚实现了一个方向更改功能 - 例如当布局从纵向变为横向(反之亦然)时。如何检测方向更改事件何时完成。
OrientationEventListener
不起作用。如何获取有关布局方向更改事件的通知?
最佳答案
使用 Activity 的 onConfigurationChanged 方法。 见以下代码:
@Override
public void onConfigurationChanged(@NotNull 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();
} else if (newConfig.orientation == Configuration.ORIENTATION_PORTRAIT){
Toast.makeText(this, "portrait", Toast.LENGTH_SHORT).show();
}
}
您还必须在 list 文件中编辑适当的 元素以包含 android:configChanges 请看下面的代码:
<activity android:name=".MyActivity"
android:configChanges="orientation|keyboardHidden"
android:label="@string/app_name">
注意:在 Android 3.2(API 级别 13)或更高版本中,当设备在纵向和横向之间切换时,“屏幕尺寸”也会发生变化。因此,如果您希望在针对 API 级别 13 或更高级别进行开发时防止由于方向更改而导致运行时重新启动,您必须为 API 级别 13 或更高级别声明 android:configChanges="orientation|screenSize"。 p>
希望这会对你有所帮助... :)
https://stackoverflow.com/questions/5726657/