简体   繁体   English

Android-将手机锁定为人像,但允许平板电脑同时横向和纵向

[英]Android - Lock phone to portrait but allow tablet to be both landscape and portrait

I am trying to write an app where I want the phone to be locked to portrait, but allow the tablet version to have both landscape and portrait. 我正在尝试编写一个应用程序,希望手机被锁定为纵向模式,但允许平板电脑版本同时具有横向和纵向模式。

I have created something that almost works based on all the following questions... 我根据以下所有问题创建了几乎可以正常工作的内容...

How do I lock screen orientation for phone, but not for tablet? 如何锁定手机(而不是平板电脑)的屏幕方向? (Android) How to allow both landscape/portrait for tablets only Portrait for phone, landscape for Tablet (Android-Layout) landscape mode in tablet only Locking phone in portrait mode (Android) 如何仅在平板电脑上同时使用风景/肖像 纵向手机 肖像,仅 在平板电脑上允许平板电脑风景 (Android版式) 横向模式 在纵向模式下锁定手机

What I've done so far is extend Activity into my own base Activity and to include orientation change code in both onCreate and an overwritten onConfigurationChanged 到目前为止,我所做的就是将Activity扩展到我自己的基本Activity中,并在onCreate和覆盖的onConfigurationChanged中都包括方向更改代码

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        if(!(new DeviceHelper().isTablet(this)))
        {
            setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
        }
        super.onCreate(savedInstanceState);
        setupActionBar(getActivity());


    }

    @Override
    public void onConfigurationChanged(Configuration newConfig) {
        //don't reload the current page when the orientation is changed
        if(!(new DeviceHelper().isTablet(this)))
        {
            setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
        }
        super.onConfigurationChanged(newConfig);

    }

Everything with this works fine (I overwrote the onConfigurationChanged to avoid the activity being destroyed and recreated which lead to a black screen for a fraction of a second). 一切都可以正常工作(我重写了onConfigurationChanged以避免活动被破坏和重新创建,从而导致黑屏时间不到一秒钟)。

My main problem is that if you hold the phone in landscape, then the activity remains in portrait...perfect! 我的主要问题是,如果您将手机横向放置,那么活动将保持纵向...完美! However, because the phone is in actual landscape then the layout for landscape is used, which is a layout from the tablet. 但是,由于手机处于实际风景中,因此将使用风景的布局,这是平板电脑的布局。

My understanding was that when setRequestedOrientation is called, then it should fire onConfigurationChanged instantly, however, the fragment is called first, then the layout inflated incorrectly, before then calling onConfigurationChanged, which means the landscape layout is already set. 我的理解是,当调用setRequestedOrientation时,它应该立即触发onConfigurationChanged,但是,首先调用该片段,然后布局错误地膨胀,然后再调用onConfigurationChanged,这意味着已经设置了景观布局。

I understand that I could possibly set the layout in each of my activities, based on the configuration but I don't really want to do that. 我知道我可以根据配置在每个活动中设置布局,但我并不是很想这么做。

So how can I get the app to use the appropriate layout file from the appropriate res folder, based on the setRequestedOrientation. 因此,如何根据setRequestedOrientation使应用程序从相应的res文件夹中使用合适的布局文件。

Thanks 谢谢

If your landscape layout is just for tablets, I would suggest you put it in the tablet layout folders ie: layout-sw600dp-land (for 7in tablet landscape) and layout-sw720dp-land (for 10in tablet landscape). 如果您的横向布局仅适用于平板电脑,我建议您将其放置在平板电脑布局文件夹中,即:layout-sw600dp-land(用于7英寸平板电脑横向)和layout-sw720dp-land(用于10in平板电脑横向)。 This way, the tablet layout will only be used in tablets. 这样,数位板布局将仅用于数位板。 If you do not declare a landscape layout for the phone, it will use the regular portrait layout. 如果您没有为手机声明横向布局,它将使用常规的纵向布局。 Separating layout folders for the tablet and phone layouts helps for organization. 将平板电脑和手机布局的布局文件夹分开有助于组织。

Everything with this works fine (I overwrote the onConfigurationChanged to avoid the activity being destroyed and recreated which lead to a black screen for a fraction of a second). 一切都可以正常工作(我重写了onConfigurationChanged以避免活动被破坏和重新创建,从而导致黑屏时间不到一秒钟)。

I'm pretty sure this is the root of your problem. 我很确定这是您问题的根源。 You should be able to accomplish what you are trying to do without overriding onConfigurationChanged . 您应该能够完成您要尝试执行的操作,而不会覆盖onConfigurationChanged Let the OS handle the configuration changes and provide the correct layout files. 让操作系统处理配置更改并提供正确的布局文件。

If you do this, you should be good on Tablets. 如果这样做,您应该在平板电脑上表现出色。 They should not set a particular orientation and they should open to the correct orientation for each Activity. 他们不应设置特定的方向,而应为每个活动打开正确的方向。 The phone devices will have that issue you are talking about with the quick black screen. 手机设备将出现您正在使用快速黑屏谈论的问题。 What is happening is that the Activity will open first in the orientation the phone is held in, say Landscape. 发生的情况是,“活动”将首先以手机所处的方向打开,例如“横向”。 Then it will set its orientation to Portrait in code and undergo a configuration change, recreating the Activity and causing the momentary flash you are seeing. 然后,它将在代码中将其方向设置为“纵向”并进行配置更改,重新创建“活动”并引起您看到的瞬时闪光。

There are ways you can mitigate this. 您可以通过多种方法缓解这种情况。

  • Use android:screenOrientation="behind" in the AndroidManifest.xml for each Activity. 在每个活动的AndroidManifest.xml使用android:screenOrientation="behind" This will start the Activity in the orientation that the previous Activity was in (Portrait for phones, whatever for tablets). 这将以先前活动所在的方向开始活动(手机为纵向,平板电脑为纵向)。 Then you never have to worry about the Activity getting created twice because it will always start itself in the correct orientation. 然后,您不必担心会两次创建活动,因为它总是以正确的方向启动。

  • This does not fix your very first Activity however. 但是,这不能解决您的第一个活动。 You can either deal with the launcher Activity having that flash exactly once when the app launches, or you can set the launcher Activity to android:screenOrientation="portrait" in the manifest and make sure to set tablets to setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED); 您可以在应用启动时处理带有该闪光灯的启动器活动一次,也可以在清单中将启动器活动设置为android:screenOrientation="portrait"并确保将平板电脑设置为setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED); in code. 在代码中。 This may cause that flash to occur on startup for Tablets. 这可能会导致在平板电脑启动时发生闪光。

Using just the first bullet point should be good enough, but you should tailor your implementation to your app's user experience requirements. 仅使用第一个要点就足够了,但是您应该根据应用程序的用户体验要求调整实现。

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

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