She*_*lam 183 java configuration android orientation
我只想在我的方向处于横向时设置一些标志,这样当在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);
}
Run Code Online (Sandbox Code Playgroud)
覆盖onConfigurationChanged将阻止我的layout-land xml以横向方向加载.
我只想在onCreate()中获取设备的当前方向.我怎么能得到这个?
Ebo*_*ike 456
Activity.getResources().getConfiguration().orientation
Run Code Online (Sandbox Code Playgroud)
Dan*_*iel 58
int orientation = this.getResources().getConfiguration().orientation;
if (orientation == Configuration.ORIENTATION_PORTRAIT) {
// code for portrait mode
} else {
// code for landscape mode
}
Run Code Online (Sandbox Code Playgroud)
当超类this是Context
Sak*_*iah 27
在某些设备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;
}
Run Code Online (Sandbox Code Playgroud)
并使用
if (orientation==1) // 1 for Configuration.ORIENTATION_PORTRAIT
{ // 2 for Configuration.ORIENTATION_LANDSCAPE
//your code // 0 for Configuration.ORIENTATION_SQUARE
}
Run Code Online (Sandbox Code Playgroud)
小智 25
int rotation = getWindowManager().getDefaultDisplay().getRotation();
Run Code Online (Sandbox Code Playgroud)
这将给出正常和反向的所有方向
并像处理它一样
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;
}
Run Code Online (Sandbox Code Playgroud)
小智 12
getActivity().getResources().getConfiguration().orientation
Run Code Online (Sandbox Code Playgroud)
此命令为Portrait返回int值1,为Landscape返回2
如果有人想要获得有意义的方向描述(比如传递给onConfigurationChanged(..)那些reverseLandscape,sensorLandscape等等),只需使用即可getRequestedOrientation()
| 归档时间: |
|
| 查看次数: |
132635 次 |
| 最近记录: |