首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >如何在android活动中仅禁用反向屏幕方向

如何在android活动中仅禁用反向屏幕方向
EN

Stack Overflow用户
提问于 2015-04-01 07:43:57
回答 1查看 1.4K关注 0票数 2

在android活动中是否有任何方法可以禁用逆向景观和反向纵向定位。我用的是下面的code.but逆向景观就来了。

代码语言:javascript
运行
复制
     rotation = ((WindowManager) getSystemService(WINDOW_SERVICE)).getDefaultDisplay().getRotation();
     System.out.println("Rotation Value : " +rotation);
    if(rotation==0){
        System.out.println("portrait");
         setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);}
    if(rotation==1){
        System.out.println("landscape");
         setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);}

     if(rotation==2 )
     {
         System.out.println("reverse portrait");
         setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LOCKED);
     }
     if(rotation==3)
     {
         System.out.println("reverse landscape");
         setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LOCKED);
     }
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2015-04-01 07:54:05

将android:configChanges="keyboardHidden|orientation“添加到AndroidManifest.xml中。这将告诉系统您将自己处理的配置更改,在本例中,不做任何操作。

代码语言:javascript
运行
复制
<activity
android:name="MainActivity"
android:screenOrientation="portrait" //This line only if you want to lock your screen Orientation
android:configChanges="keyboardHidden|orientation|screenSize">

有关更多细节,请查看http://developer.android.com/reference/android/R.attr.html#configChanges

然后重写onConfigurationChanged:http://developer.android.com/guide/topics/resources/runtime-changes.html

代码语言:javascript
运行
复制
    @Override
    public void onConfigurationChanged(Configuration newConfig) {
        super.onConfigurationChanged(newConfig);

        // Checks the orientation of the screen
        if (newConfig.orientation == Configuration.ORIENTATION_LANDSCAPE) {
            Toast.makeText(this, "landscape", Toast.LENGTH_SHORT).show();
        } else if (newConfig.orientation == Configuration.ORIENTATION_PORTRAIT){
            Toast.makeText(this, "portrait", Toast.LENGTH_SHORT).show();
        }
    }
票数 3
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/29385404

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档