简体   繁体   English

如何在Android的firebase UI中将方向设置为纵向

[英]How to set the orientation to portrait in firebase UI in Android

I am using firebase UI for authentication, In case of ioS the orientation is not an issue, In case of android if the screen orientation of phone is auto rotated, the firebase UI will also get rotated.我正在使用 firebase UI 进行身份验证,在 ios 的情况下,方向不是问题,如果是 android,如果手机的屏幕方向是自动旋转的,firebase UI 也会旋转。

  • I have set the application orientation to portrait in manifest我已在清单中将应用程序方向设置为纵向
  • I have also added code to make my activity portrait in my class.我还添加了代码来在我的班级中制作我的活动肖像。

Setting a custom UI to the Firebase UI with style将自定义 UI 设置为具有样式的 Firebase UI

    <style name="FirebaseLoginTheme" parent="FirebaseUI">
    <item name="android:screenOrientation">portrait</item>
    <item name="android:windowContentOverlay">@null</item>
    </style>

does not worked.不起作用。 Is their any way to restrict it to portrait.他们有什么方法可以将其限制为肖像。

add your a orientation in manifest file like this :在清单文件中添加您的方向,如下所示:

<activity
       android:name=".YourActivity"
       android:screenOrientation="portrait"
       android:theme="@style/FirebaseLoginTheme" />

Your Style你的风格

  <style name="FirebaseLoginTheme" parent="FirebaseUI">
   <item name="android:windowContentOverlay">@null</item>
  </style>

I've solved this problem by setting the portrait mode programmatically in every activity.我通过在每个活动中以编程方式设置纵向模式解决了这个问题。 If you target Android 8+ you may get an error 'Only fullscreen activities can request orientation' from some com.firebase.ui.auth.ui activities and that's why I've used a try catch but your login activity will be still locked to the portrait mode.如果您的目标是 Android 8+,您可能会从某些 com.firebase.ui.auth.ui 活动中收到错误“只有全屏活动可以请求方向”,这就是为什么我使用了 try catch 但您的登录活动仍将锁定为肖像模式。

Add this in your application class and remember to include it in your manifest's application tag ( android:name=".MyApplication" )将此添加到您的应用程序类中,并记住将其包含在清单的应用程序标记中( android:name=".MyApplication"

public class MyApplication extends Application{


public MyApplication() {

}


    registerActivityLifecycleCallbacks(new ActivityLifecycleCallbacks() {
        @Override
        public void onActivityCreated(Activity activity, Bundle bundle) {

            try {
                activity.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
            }catch (Exception e){
            }

        }

        @Override
        public void onActivityStarted(Activity activity) {

        }

        @Override
        public void onActivityResumed(Activity activity) {

        }

        @Override
        public void onActivityPaused(Activity activity) {

        }

        @Override
        public void onActivityStopped(Activity activity) {

        }

        @Override
        public void onActivitySaveInstanceState(Activity activity, Bundle bundle) {

        }

        @Override
        public void onActivityDestroyed(Activity activity) {

        }
    });
}

}

add this to your manifest :将此添加到您的清单中:

 <activity
            android:screenOrientation="landscape"
            android:name="com.firebase.ui.auth.KickoffActivity"
            tools:replace="android:theme"
            android:theme="@style/AppTheme" />

And FYI this is my AppTheme (this exists in style.xml)仅供参考,这是我的 AppTheme(它存在于 style.xml 中)

    <style name="AppTheme" parent="@style/Theme.AppCompat.Light.NoActionBar">
    <item name="android:windowBackground">@android:color/transparent</item>
    <item name="android:colorBackgroundCacheHint">@null</item>
    <item name="android:windowAnimationStyle">@android:style/Animation</item>
    <item name="android:windowNoTitle">true</item>
    <item name="android:windowContentOverlay">@null</item>
    <item name="android:windowFullscreen">true</item>
</style>

Just call setLockOrientation(true) on AuthUI.SignInIntentBuilder object只需在AuthUI.SignInIntentBuilder对象上调用setLockOrientation(true)

For example:例如:

startActivityForResult(auth.createSignInIntentBuilder().setAvailableProviders(idProviders).setLockOrientation(true).build(), RC_SIGN_IN);

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

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