我只是想设置一些标志时,我的方向是横向这样,当活动的onCreate被重建()我可以与肖像如何加载或横向切换。我已经有一个布局,土地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会阻止我的布局,土地从XML横向装。
我只想得到我的设备中的onCreate当前方位()。我怎样才能得到呢?
Activity.getResources().getConfiguration().orientation
int orientation = this.getResources().getConfiguration().orientation;
if (orientation == Configuration.ORIENTATION_PORTRAIT) {
// code for portrait mode
} else {
// code for landscape mode
}
当this
的超类是Context
在一些设备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
}
int rotation = getWindowManager().getDefaultDisplay().getRotation();
这将赋予像正常和相反的方向都
并处理它像
int angle = 0;
switch (rotation) {
case Surface.ROTATION_90:
angle = -90;
break;
case Surface.ROTATION_180:
angle = 180;
break;
case Surface.ROTATION_270:
angle = 90;
break;
default:
angle = 0;
break;
}
getActivity().getResources().getConfiguration().orientation
对于肖像此命令将返回int值1和2为景观
如果有人想获得meaningful方向说明(类似于通过与onConfigurationChanged(..)
,reverseLandscape
等方面sensorLandscape
),只需使用getRequestedOrientation()
在您的活动类使用下面的方法:
@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");
}
}
然后,宣布你的活动处理的配置变化,在你的清单文件编辑相应的元素,包括与代表要处理配置的值android:configChanges
属性。可能的值都为android:configChanges
属性的文件中列出(最常用的值是“方向”,以防止重新启动时屏幕方向的变化和“keyboardHidden”,以防止重新启动时键盘的可用性的变化)。 |您可以通过用一个管分离声明中的属性多个配置值字符。
<activity android:name=".MyActivity"
android:configChanges="orientation|keyboardHidden"
android:label="@string/app_name">
就这样!!
如果你希望覆盖onConfigurationChanged方法,仍然希望它做的话,基本的东西考虑使用super.onConfigyrationChanged()作为覆盖方法的第一条语句。
我只是想提出另一种替代方案,将关注一些你:
android:configChanges="orientation|keyboardHidden"
意味着我们明确声明的布局将被注入。
如果我们希望保持自动注射感谢布局,用地和布局的文件夹。所有你需要做的就是将它添加到的onCreate:
if (getResources().getConfiguration().orientation == Configuration.ORIENTATION_LANDSCAPE) {
getSupportActionBar().hide();
} else if (getResources().getConfiguration().orientation == Configuration.ORIENTATION_PORTRAIT) {
getSupportActionBar().show();
}
在这里,我们显示或不动作条根据手机的方向