本文介绍了除去定向restricitons编程的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的清单我设置仅限于纵向的活动。但我需要删除条件此限制。所以,我怎么编程实现去除方向的限制?

UPD:我的present设置是:

 <活动
        机器人:名字=。activity.MainActivity
        机器人:标签=@字符串/ APP_NAME
        机器人:主题=@安卓风格/ Theme.NoTitleBar
        机器人:screenOrientation =画像
        机器人:configChanges =方向>
 / **
 *定义所使用的设备是否是片剂,如果是这样增加了水平方向的选项。
 * /
     保护无效_updateScreenOrientationModes(){
         如果(((所有MyApplication)getApplication())._ isTablet == true)而
                setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED);
         }


解决方案

无论你是否已经设置的android:screenOrientation 在你的清单,你可以通过编程设定的方向与Activity.setRequestedOrientation().

在本质上,删除限制由

完成

  setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED);

在此之后,活动将展出变化的屏幕方向的默认行为当物理设备方向的变化。

这是可能的,但是,要在当前的物理设备的方向在同一时间竟相匹配。我还没有尝试过,但我pretty相信如果你所做的一切是上述情况,如果该设备是身体在横向,当你做到了,那么你会留在纵向模式下,直到你身体移动设备纵向和回的风景线。

我会做的是一定要设置的android:configChanges =方向在你的清单,并重写Activity.onConfigurationChanged(),您可以在其中,根据你的情况,无论是执行的方向改变或缓存的方向。然后,每当您的病情变化,你就会有得心应手当前的物理方向,以便在需要时可以在这一点上改变它。

In my manifest I've setup an activity restricted to portrait orientation. But I need to remove this restriction on condition. So, how do I achieve removing orientation restrictions programmatically ?

upd: my present settings are:

    <activity
        android:name=".activity.MainActivity"
        android:label="@string/app_name"
        android:theme="@android:style/Theme.NoTitleBar"
        android:screenOrientation="portrait"
        android:configChanges="orientation">


 /**
 * Defines whether the device being used is a tablet and if so adds horizontal orientation option.
 */
     protected void _updateScreenOrientationModes(){
         if(((MyApplication) getApplication())._isTablet == true)
                setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED);
         }
解决方案

Whether or not you've set android:screenOrientation in your Manifest, you can programmatically set the orientation with Activity.setRequestedOrientation().

In essence, "removing the restriction" is accomplished by

    setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED);

after which the Activity will exhibit the default behavior of changing screen orientation when the physical device orientation changes.

It's likely, however, that you want to actually match the current physical device orientation at the same time. I haven't tried it, but I'm pretty sure that if all you did was the above, if the device was physically in a landscape orientation when you did it, then you'd stay in portrait mode until you physically moved the device to portrait and back to landscape.

What I would do is be sure to set android:configChanges="orientation" in your Manifest and override Activity.onConfigurationChanged(), in which you can, according to your condition, either perform the orientation change or cache the orientation. Then whenever your condition changes, you'll have the current physical orientation handy so you can change it at that point if necessary.

这篇关于除去定向restricitons编程的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-05 19:06
查看更多