Android 屏幕处于横向模式时如何避免吐司
How to avoid the toast when screen is on landscape mode in Android
我需要使用 toast 或 snackbar 向用户显示消息。但是,我不想在用户倾斜 his/her phone 进入横向模式时显示此消息。我怎样才能实现这条消息只在纵向模式下显示而不在横向模式下显示?
您可以在资源配置对象中检查 phone 的方向:
getResources().getConfiguration().orientation
当它等于 ORIENTATION_LANDSCAPE
时,根本不显示 toast/snackbar
参考资料见:
https://developer.android.com/reference/android/content/res/Configuration.html#orientation
此方法将 return true
如果 phone 是纵向的, false
如果 phone 是横向的,它使用配置:
private boolean isPortrait(){
return (getResources().getConfiguration().orientation==ORIENTATION_PORTRAIT);
}
在 Android Studio 中导入配置 类 非常简单。因此,从这里您可以使用上面给出的方法使用简单的 if 语句:
if(isPortrait()){
//Show you Toast and snackbar here
}
有关此内容的更多信息check the official documentation here。
我需要使用 toast 或 snackbar 向用户显示消息。但是,我不想在用户倾斜 his/her phone 进入横向模式时显示此消息。我怎样才能实现这条消息只在纵向模式下显示而不在横向模式下显示?
您可以在资源配置对象中检查 phone 的方向:
getResources().getConfiguration().orientation
当它等于 ORIENTATION_LANDSCAPE
时,根本不显示 toast/snackbar
参考资料见: https://developer.android.com/reference/android/content/res/Configuration.html#orientation
此方法将 return true
如果 phone 是纵向的, false
如果 phone 是横向的,它使用配置:
private boolean isPortrait(){
return (getResources().getConfiguration().orientation==ORIENTATION_PORTRAIT);
}
在 Android Studio 中导入配置 类 非常简单。因此,从这里您可以使用上面给出的方法使用简单的 if 语句:
if(isPortrait()){
//Show you Toast and snackbar here
}
有关此内容的更多信息check the official documentation here。