简体   繁体   English

如何锁定某些屏幕尺寸的方向?

[英]how to lock orientation for some screen sizes?

i am developing an application which will run on all devices ranging from 4.7 inch devices and above both in portrait and landscape orientation. 我正在开发一个应用程序,该应用程序将在4.7英寸设备及以上的所有设备上以纵向和横向运行。

Now the issue is, i have around 50 or so activities and i want to lock orientation to vertical if the device screen size is less than equal to 5 inch . 现在的问题是,我大约有50项活动,如果设备屏幕尺寸小于5英寸 ,我想将方向锁定为垂直。 I can set orientation lock using the code below :- 我可以使用以下代码设置方向锁定:-

setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_SENSOR_LANDSCAPE);
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_SENSOR_PORTRAIT);

Above code i can use to lock orientation and below code for display metrics :- 上面的代码我可以用来锁定方向,下面的代码可以用于显示指标:-

Display display = getWindowManager().getDefaultDisplay();
Point size = new Point();
display.getSize(size);
int width = size.x;
int height = size.y;

or 要么

WindowManager wm = (WindowManager) ctx.getSystemService(Context.WINDOW_SERVICE);
Display display = wm.getDefaultDisplay();

Now my question is, that what is the appropriate method to lock orientation for devices <= 5 inch and do i have to write this code in all my activities ? 现在我的问题是,锁定小于等于5英寸的设备的方向的合适方法是什么,我是否必须在所有活动中编写此代码? Also i cannot extend using baseActivity because most of the activities are already extending custom listener classes 我也不能使用baseActivity进行扩展,因为大多数活动已经在扩展自定义侦听器类

Create a base Activity class that has the code. 创建具有代码的基本Activity类。 The other 50 extend it. 其他50个扩展它。

Create base activity all your 50 activities extend. 创建基本活动,这50个活动都将扩展。 put your locking logic in onResume() or onCreate() of that base activity and lock the orientation there. 将锁定逻辑放在该基本活动的onResume()onCreate()中,然后将方向锁定在那里。

BTW: 50 activities is really a lot. 顺便说一句:50个活动确实很多。 Tried fragments? 试过碎片?

One of the best OOP's practices that always create a "BaseActivity" or "BaseFragmentActivity" to take the benefit of code reusability and less redundant. 始终创建“ BaseActivity”或“ BaseFragmentActivity”以利用代码可重用性和较少冗余的优势的最佳OOP实践之一。 The standard way to restrict the orientation of an activity in the manifest. 限制清单中活动方向的标准方法。

But you've have to restrict based on conditions and also don't want to re-write in every activity. 但是您必须根据条件进行限制,也不想在每个活动中都进行重写。 So my suggestion to create "Base Activity" that extends to all of your sub activities. 因此,我建议创建扩展到所有子活动的“基础活动”。

public class BaseActivity extends Activity {
    public void onCreate(Bundle savedState) {
        //...

        if(isScreenLarge()) {
            // width > height, better to use Landscape
            setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
        } else {
            setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
        }
    }

    public boolean isScreenLarge() {
        final int screenSize = getResources().getConfiguration().screenLayout
                & Configuration.SCREENLAYOUT_SIZE_MASK;
        return screenSize == Configuration.SCREENLAYOUT_SIZE_LARGE
                || screenSize == Configuration.SCREENLAYOUT_SIZE_XLARGE;
    }
}

Now extend all your activities with BaseActivity . 现在,使用BaseActivity扩展所有活动。

Hope this helps. 希望这可以帮助。

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

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