如何在android中设置非主要活动的屏幕方向

时间:2018-03-06 13:27:51

标签: java android screen-orientation

我是Android开发的初学者

我想从非主screen orientation

设置activity

我有两节课: -

  1. 公共课MainActivity延伸AppCompatActivity
  2. MainActivity中有method: -

    public void setOrintation(){
    
    if (My_Values.STABLE_MODE_ENABLE == 1) {
    
            int orientation = this.getRequestedOrientation();
            int rotation = ((WindowManager) this.getSystemService(
                    Context.WINDOW_SERVICE)).getDefaultDisplay().getRotation();
            switch (rotation) {
                case Surface.ROTATION_0:
                    orientation = ActivityInfo.SCREEN_ORIENTATION_PORTRAIT;
                    break;
                case Surface.ROTATION_90:
                    orientation = ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE;
                    break;
                case Surface.ROTATION_180:
                    orientation = ActivityInfo.SCREEN_ORIENTATION_REVERSE_PORTRAIT;
                    break;
                default:
                    orientation = ActivityInfo.SCREEN_ORIENTATION_REVERSE_LANDSCAPE;
                    break;
            }
            this.setRequestedOrientation(orientation);
        } else {
            this.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED);
        }
    }
    
    1. public class My_View extends View implements View.OnTouchListener(非内部课程)
    2. 然后我无法调用MainActivity setOrintation()方法

      如何做到这一点?

1 个答案:

答案 0 :(得分:0)

也许您可以尝试使用metod setOrintation()创建接口,并让MainActivity实现此接口。在My_View对象中,您必须以此接口

发送活动