Activity orientation changes automatically on Android

前端 未结 7 2076
天命终不由人
天命终不由人 2020-12-10 01:50

I\'m developing a mobile application based on Android with minSdkVersion=15. I would like to support both orientations for tablets and only portrait for smartph

7条回答
  •  孤城傲影
    2020-12-10 02:52

    In your base activity you can put something like this:

    /*  Device types. */
    static int MOBILE_DEVICE = 0;
    static int SEVEN_INCH_TABLET = 1;
    static int TEN_INCH_TABLET = 2;
    
    private static int deviceType = MOBILE_DEVICE;
    private boolean deviceTypeDetermined = false;
    
    
    @RequiresApi(api = Build.VERSION_CODES.JELLY_BEAN_MR1)
    @Override
    protected void onCreate(Bundle savedInstanceState) {
    
        if ( ! deviceTypeDetermined) setDeviceType();
    
        /* Screen rotation only for tablets. */
        if (getDeviceType() < SEVEN_INCH_TABLET) {
            setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
            lockScreenOrientation();
    
        ......
    }
    
    
    
    // ---------------------------------------------------------------------------------------------
    static int getDeviceType() {
        return deviceType;
    }
    // ---------------------------------------------------------------------------------------------
    
    
    // ---------------------------------------------------------------------------------------------
    private void setDeviceTypeDetermined() {
        this.deviceTypeDetermined = true;
    }
    // ---------------------------------------------------------------------------------------------
    
    
    // ---------------------------------------------------------------------------------------------
    @RequiresApi(api = Build.VERSION_CODES.JELLY_BEAN_MR1)
    private void setDeviceType() {
    
        /*
            Let's invest on what kind of screen our APP is invited...
    
            We only make a difference in 10", 7" tablets and the rest...
        */
    
        Display mDisplay = getWindowManager().getDefaultDisplay();
        Point mScreenResolution = new Point();
    
        mDisplay.getRealSize(mScreenResolution);
    
        int mWidthPixels = mScreenResolution.x;
        int mHeightPixels = mScreenResolution.y;
    
        DisplayMetrics mMetrics = new DisplayMetrics();
        getWindowManager().getDefaultDisplay().getMetrics(mMetrics);
    
        float mWidthDpi = mMetrics.xdpi;
        float mHeightDpi = mMetrics.ydpi;
    
        float mWidthInches = mWidthPixels / mWidthDpi;
        float mHeightInches = mHeightPixels / mHeightDpi;
    
    
    
        double mDiagonalInches = Math.sqrt(
                (mWidthInches * mWidthInches)
                        + (mHeightInches * mHeightInches));
    
    
        if (mDiagonalInches >= 9) {
    
            /*
                A tablet with 8" x 5" is called a 10", but it is in fact
                9.43398". Interesting that this kind of things happens in
                the world of informatics.... ;)
            */
    
            MyBaseAppCompatActivity.deviceType = TEN_INCH_TABLET;
        }
    
        else if (mDiagonalInches >= 7) {
            MyBaseAppCompatActivity.deviceType = SEVEN_INCH_TABLET;
        }
    
        else
        {
            MyBaseAppCompatActivity.deviceType = MOBILE_DEVICE;
        }
    
    
        setDeviceTypeDetermined();
    }
    // ---------------------------------------------------------------------------------------------
    
    
    // ---------------------------------------------------------------------------------------------
    void lockScreenOrientation() {
    
        /*  Screen rotation only for tablets. */
        if (deviceType < SEVEN_INCH_TABLET ) return;
    
        setRequestedOrientation(getResources().getConfiguration().orientation);
    }
    // ---------------------------------------------------------------------------------------------
    
    
    // ---------------------------------------------------------------------------------------------
    void unlockScreenOrientation() {
    
        /*  Screen rotation only for tablets. */
        if (deviceType < SEVEN_INCH_TABLET ) return;
    
        setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_SENSOR);
    }
    // ---------------------------------------------------------------------------------------------
    

    And in your activities in the onCreate:

    // ---------------------------------------------------------------------------------------------
    @RequiresApi(api = Build.VERSION_CODES.JELLY_BEAN_MR1)
    protected void onCreate(Bundle savedInstanceState) {
        // -----------------------------------------------------------------------------------------
        super.onCreate(savedInstanceState);
    
        if (getDeviceType() >= SEVEN_INCH_TABLET) unlockScreenOrientation();
    
        setContentView(R.layout.main_activity);
    
        .........
    
    // ---------------------------------------------------------------------------------------------
    

提交回复
热议问题