gpt4 book ai didi

android - 在 Android 上运行时禁用屏幕旋转

转载 作者:太空宇宙 更新时间:2023-11-03 11:27:23 28 4
gpt4 key购买 nike

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);
}
}

}

我的应用程序如上,并在 xml 中添加 android:configChanges="orientation"。当我的应用程序启动时,我的屏幕是纵向的,我按 ctrl+F12,屏幕也旋转,屏幕是横向的,第二次我按 ctrl +F12,屏幕也旋转,屏幕是PORTRAIT,然后我按ctrl+F12,屏幕保持PORTRAIT。所以我再次按下,屏幕保持肖像。我的问题是为什么我的屏幕在应用程序启动时不保持纵向。

编辑:我想用代码来控制屏幕旋转,我可以这样做吗?

最佳答案

如果您想让您的应用程序始终处于纵向模式,您可以将这行添加到 list 中的元素

android:screenOrientation="portrait"

在 AndroidManifest.xml 文件的每个 Activity 中添加这一行。

关于android - 在 Android 上运行时禁用屏幕旋转,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5564211/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com