简体   繁体   English

android将系统方向锁定为横向(而不仅仅是我的应用程序)

[英]android lock SYSTEM orientation to landscape (not just my application)

i want to give the user the option to set the phone's orientation to auto, portrait or landscape.which means for the ENTIRE PHONE's orientation not just my application. 我想为用户提供将手机方向设置为自动,纵向或横向的选项。这意味着整个电话的方向不只是我的应用程序。 every other app must have the landscape orientation too. 每个其他应用程序也必须具有横向方向。 it was pretty simple to do the first two like this 这样的前两个很简单

portrait: 肖像:

Settings.System.putInt( this.getContentResolver(), Settings.System.ACCELEROMETER_ROTATION, 0);  

auto: 汽车:

Settings.System.putInt( this.getContentResolver(), Settings.System.ACCELEROMETER_ROTATION, 1); 

but i can't find a way to set it to landscape. 但我找不到将其设置为横向的方法。 i want to set all apps to landscape not just mine. 我想将所有应用程序设置为横向模式,而不仅仅是我的。 i see here that it can be done but it doesn't give much information. 我在这里看到可以做到,但没有提供太多信息。 How to make Android system force LANDSCAPE for all apk? 如何使Android系统对所有apk强制使用LANDSCAPE? . anyone willing to explain or provide code snippet? 有人愿意解释或提供代码段吗?

Code below can force your screen to landscape mode. 下面的代码可以强制您的屏幕进入横向模式。 It is similar to set it to other mode. 类似于将其设置为其他模式。

public class MyService extends Service {
    private boolean mViewAdded = false;

    private WindowManager.LayoutParams mLayoutParams;
    private View mOverlayView;
     private WindowManager mWindowManager;

    // Call this some where in your code
    private void setLandscapeOrientation() {
        mWindowManager = ((WindowManager)getSystemService("window"));
        mOverlayView = new View(this);
        mLayoutParams = new WindowManager.LayoutParams();
        mLayoutParams.type = WindowManager.LayoutParams.TYPE_SYSTEM_ERROR;
        mLayoutParams.width = 0;
        mLayoutParams.height = 0;
        mLayoutParams.flags = mLayoutParams.flags |
            WindowManager.LayoutParams.FLAG_HARDWARE_ACCELERATED |
            WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE |
            WindowManager.LayoutParams.FLAG_NOT_TOUCHABLE;
        mLayoutParams.flags = mLayoutParams.flags &
            ~WindowManager.LayoutParams.FLAG_TURN_SCREEN_ON &
            ~WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON;
        mLayoutParams.screenOrientation = ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE;
        if (mViewAdded) {
            mWindowManager.updateViewLayout(mOverlayView, mLayoutParams);
        } else {
            mWindowManager.addView(mOverlayView, mLayoutParams);
            mViewAdded = true;
        }
    }

    @Override
    public void onDestroy() {
        super.onDestroy();

        if (mViewAdded) {
            mWindowManager.removeView(mOverlayView);
            mViewAdded = false;
        }
    }
}

In your AndroidManifest.xml, for each activity put 在您的AndroidManifest.xml中,为每个活动放置

android:screenOrientation="landscape"

It forces the activity to landscape 它迫使活动景观化

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM