Dev*_*Kid 3 android landscape portrait orientation screen-orientation
我正在开发一个应用程序,我需要在其中提供不同方向的不同活动的背景图像。所以我以这种方式接近:
@Override
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
setLanguage();
// Checks the orientation of the screen
if (newConfig.orientation == Configuration.ORIENTATION_LANDSCAPE) {
Toast.makeText(this, "landscape", Toast.LENGTH_SHORT).show();
// set background for landscape
} else if (newConfig.orientation == Configuration.ORIENTATION_PORTRAIT) {
Toast.makeText(this, "portrait", Toast.LENGTH_SHORT).show();
// set background for portrait
}
}
Run Code Online (Sandbox Code Playgroud)
和
android:configChanges="locale|orientation|screenSize"
Run Code Online (Sandbox Code Playgroud)
并在onCreate()假设用户将在纵向模式下启动应用程序时,我为纵向设置了背景图像。
一切正常,因为当用户改变他们的模式时,相应的背景被设置等等。
但是,如果用户在手机处于横向模式时启动此应用程序,正如我在用户将以纵向模式启动应用程序之前假设的那样,在第一次启动时显示的纵向图像。
那么我该如何解决这个问题呢?一句话,为不同的方向设置不同的背景图像的最佳方法是什么?我在正确的轨道上吗?
在onCreate您的活动中,您可以使用 this 检查当前方向
Configuration newConfig = getResources().getConfiguration();
if (newConfig.orientation == Configuration.ORIENTATION_LANDSCAPE) {
Toast.makeText(this, "landscape", Toast.LENGTH_SHORT).show();
// set background for landscape
} else if (newConfig.orientation == Configuration.ORIENTATION_PORTRAIT) {
Toast.makeText(this, "portrait", Toast.LENGTH_SHORT).show();
// set background for portrait
}
Run Code Online (Sandbox Code Playgroud)