簡體   English   中英

如何從Android中的非主要活動設置屏幕方向

[英]how to set screen orientation from non main activity in android

我是android開發的初學者

我想通過非主要activity設置screen orientation

我有2節課:-

  1. 公共類MainActivity擴展了AppCompatActivity

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 (類內無)

那我就不能調用MainActivity setOrintation()方法

怎么做呢?

也許您可以嘗試使用metod setOrintation()創建接口,並讓MainActivity實現此接口。 在My_View對象中,您將必須發送活動作為此接口

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM