在Android上禁用运行时的屏幕旋转 公共类testScreenRotation扩展活动{ /**在首次创建活动时调用*/ 私人的国际性取向; 私有布尔mDisableScreenRotation=true; @凌驾 创建时的公共void(Bundle savedInstanceState){ super.onCreate(savedInstanceState); mRuntimeOrientation=this.getScreenOrientation(); setContentView(R.layout.main); } 受保护的int getScreenOrientation(){ /* Display Display=getWindowManager().getDefaultDisplay(); int-orientation=display.getOrientation(); if(方向==配置。方向\未定义){ 方向=getResources().getConfiguration().orientation; if(方向==配置。方向\未定义){ if(display.getWidth()==display.getHeight()) 方向=配置。方向\方形; else if(display.getWidth()
我的应用程序如上所述,并在xml中添加android:configChanges=“orientation”。当我的应用程序启动时,我的屏幕是纵向的,我按ctrl+F12,屏幕也旋转,屏幕是横向的,然后我按ctrl+F12,屏幕也旋转,屏幕是纵向的,然后我按ctrl+F12,屏幕保持纵向。所以我再按一次,屏幕保持纵向。我的问题是,为什么我的屏幕在应用程序启动时不保持纵向在Android上禁用运行时的屏幕旋转 公共类testScreenRotation扩展活动{ /**在首次创建活动时调用*/ 私人的国际性取向; 私有布尔mDisableScreenRotation=true; @凌驾 创建时的公共void(Bundle savedInstanceState){ super.onCreate(savedInstanceState); mRuntimeOrientation=this.getScreenOrientation(); setContentView(R.layout.main); } 受保护的int getScreenOrientation(){ /* Display Display=getWindowManager().getDefaultDisplay(); int-orientation=display.getOrientation(); if(方向==配置。方向\未定义){ 方向=getResources().getConfiguration().orientation; if(方向==配置。方向\未定义){ if(display.getWidth()==display.getHeight()) 方向=配置。方向\方形; else if(display.getWidth(),android,Android,我的应用程序如上所述,并在xml中添加android:configChanges=“orientation”。当我的应用程序启动时,我的屏幕是纵向的,我按ctrl+F12,屏幕也旋转,屏幕是横向的,然后我按ctrl+F12,屏幕也旋转,屏幕是纵向的,然后我按ctrl+F12,屏幕保持纵向。所以我再按一次,屏幕保持纵向。我的问题是,为什么我的屏幕在应用程序启动时不保持纵向 编辑:我想使用代码来控制屏幕漫游,如果我能做到这一点的话?如果你想让你的应用程序一直处于纵向模式,你可以将这一行添加到清单中的
编辑:我想使用代码来控制屏幕漫游,如果我能做到这一点的话?如果你想让你的应用程序一直处于纵向模式,你可以将这一行添加到清单中的元素
public class testScreenRotation extends Activity {
/** Called when the activity is first created. */
private int mRuntimeOrientation;
private boolean mDisableScreenRotation=true;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
mRuntimeOrientation = this.getScreenOrientation();
setContentView(R.layout.main);
}
protected int getScreenOrientation() {
/*
Display display = getWindowManager().getDefaultDisplay();
int orientation = display.getOrientation();
if (orientation == Configuration.ORIENTATION_UNDEFINED) {
orientation = getResources().getConfiguration().orientation;
if (orientation == Configuration.ORIENTATION_UNDEFINED) {
if (display.getWidth() == display.getHeight())
orientation = Configuration.ORIENTATION_SQUARE;
else if(display.getWidth() < display.getHeight())
orientation = Configuration.ORIENTATION_PORTRAIT;
else
orientation = Configuration.ORIENTATION_LANDSCAPE;
}
}
return orientation;
*/
return Configuration.ORIENTATION_PORTRAIT;
}
@Override
public void onConfigurationChanged(Configuration newConfig) {
// TODO Auto-generated method stub
if (mDisableScreenRotation) {
super.onConfigurationChanged(newConfig);
this.setRequestedOrientation(mRuntimeOrientation);
} else {
mRuntimeOrientation = this.getScreenOrientation();
super.onConfigurationChanged(newConfig);
}
}
}
在AndroidManifest.xml文件中的每个活动中添加此行。更改AndroidManifest.xml文件。够了
现在,您的应用程序将不会更改方向。我相信您正在寻找的方法是
Activity.setRequestedOrientation(ActivityInfo.SCREEN\u Orientation)
连同
android:configChanges=“orientation”
对于清单中告诉系统您将自己处理定向的活动,应禁用自动重新定向。,此链接解决了我的问题,即setRequestedOrientation(ActivityInfo.SCREEN\u orientation\u Grait);而且不需要在xml中添加行android:configChanges=“orientation”。因此我认为我不能使用配置来获取屏幕方向,应该使用ActivityInfo。您也可以从java源代码在运行时禁用屏幕旋转。
就这样试试
android:screenOrientation="portrait"
现在尝试在应用程序运行时调用此方法。可能在某些方法中或在某些按钮上。private void setAutoRotation(final boolean autorotate) {
AsyncTask.execute(new Runnable() {
public void run() {
try {
IWindowManager wm = IWindowManager.Stub.asInterface(ServiceManager.getService(Context.WINDOW_SERVICE));
if (autorotate) {
wm.thawRotation();
} else {
wm.freezeRotation(-1);
}
} catch (RemoteException exc) {
Log.secW(TAG, "Unable to save auto-rotate setting");
}
}
});
}
谢谢你的回答。我也可以用代码保持屏幕portrait@pengwang它解决了你的问题吗?如果是,请接受它作为答案。谢谢,我应该解决我的问题,你可以测试我的答案。谢谢你的回答。虽然整个
android:configChanges=“orientation”
技巧没有奏效,但这对我来说还是有效的。干杯这个答案为整个活动设置方向,不是在运行时而是在编译时。。。不确定为什么这被认为是一个有效的答案,但它应该被更新以反映它真正的功能。感谢您的回答,我希望在运行时使用代码禁用屏幕旋转。我已经解决了这个问题,但我不需要android:configChanges=“orientation”
setAutoRotation(false);
setAutoRotation(true);