我只是想设置一些标志,当我的方向是在景观,以便当活动在onCreate()中重新创建时,我可以在纵向和景观之间切换加载什么。我已经有一个layout-land xml处理我的布局。
public void onConfigurationChanged(Configuration _newConfig) {
if (_newConfig.orientation == Configuration.ORIENTATION_LANDSCAPE) {
this.loadURLData = false;
}
if (_newConfig.orientation == Configuration.ORIENTATION_PORTRAIT) {
this.loadURLData = true;
}
super.onConfigurationChanged(_newConfig);
}
覆盖onConfigurationChanged将阻止我的layout-land xml在横向加载。
我只是想在onCreate()中获得我的设备的当前方向。我怎么才能得到这个?
在你的活动类中使用下面的方法:
@Override
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
// Checks the orientation of the screen
if (newConfig.orientation == Configuration.ORIENTATION_LANDSCAPE) {
setlogo();// Your Method
Log.d("Daiya", "ORIENTATION_LANDSCAPE");
} else if (newConfig.orientation == Configuration.ORIENTATION_PORTRAIT) {
setlogoForLandScape();// Your Method
Log.d("Daiya", "ORIENTATION_PORTRAIT");
}
}
Then to declare that your activity handles a configuration change, edit the appropriate element in your manifest file to include the android:configChanges attribute with a value that represents the configuration you want to handle. Possible values are listed in the documentation for the android:configChanges attribute (the most commonly used values are "orientation" to prevent restarts when the screen orientation changes and "keyboardHidden" to prevent restarts when the keyboard availability changes). You can declare multiple configuration values in the attribute by separating them with a pipe | character.
<activity android:name=".MyActivity"
android:configChanges="orientation|keyboardHidden"
android:label="@string/app_name">
这就是! !
在某些设备中,void onConfigurationChanged()可能会崩溃。用户将使用此代码获取当前屏幕方向。
public int getScreenOrientation()
{
Display getOrient = getActivity().getWindowManager().getDefaultDisplay();
int orientation = Configuration.ORIENTATION_UNDEFINED;
if(getOrient.getWidth()==getOrient.getHeight()){
orientation = Configuration.ORIENTATION_SQUARE;
} else{
if(getOrient.getWidth() < getOrient.getHeight()){
orientation = Configuration.ORIENTATION_PORTRAIT;
}else {
orientation = Configuration.ORIENTATION_LANDSCAPE;
}
}
return orientation;
}
和使用
if (orientation==1) // 1 for Configuration.ORIENTATION_PORTRAIT
{ // 2 for Configuration.ORIENTATION_LANDSCAPE
//your code // 0 for Configuration.ORIENTATION_SQUARE
}