[英]Not changing the orientation which has entered
在我的应用中,您可以在两个方向上都输入一个活动,但我希望在一个屏幕上输入,如果您以一个方向输入,则不能更改为另一个方向,直到退出该活动
You can tell the system that your activity will handle a configuration change, such as an orientation change. 您可以告诉系统您的活动将处理配置更改,例如方向更改。
Edit your <activity>
element in your manifest file to include the android:configChanges
attribute with a value that represents the configuration you want to handle: 编辑清单文件中的
<activity>
元素,以将android:configChanges
属性包含一个代表您要处理的配置的值:
<activity android:name=".MyActivity" android:configChanges="orientation">
When your activity starts -lets say on onStart()
or onResume()
, capture what is the current orientation, so you can use that value during the execution of the activity to make any decision based on that 活动开始时-在
onStart()
或onResume()
,捕获当前方向,因此您可以在活动执行期间使用该值来基于该方向做出任何决定
private int initialOrientation;
@Override
public void onResume(){
super.onResume();
initialOrientation = getResources().getConfiguration().orientation;
}
Then in the activity, define a listener for the configuration listener, in which you set the orientation to what it was, and the orientation will stay as it is. 然后在活动中,为配置侦听器定义一个侦听器,在其中将方向设置为原来的方向,并且方向将保持原样。
@Override
public void onConfigurationChanged(Configuration newConfig)
{
super.onConfigurationChanged(newConfig);
setRequestedOrientation(initialOrientation);
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.