问题
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.
- 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
<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.
回答1:
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>
回答2:
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.
Add this in your application class and remember to include it in your manifest's application tag (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) {
}
});
}
}
回答3:
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)
<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>
来源:https://stackoverflow.com/questions/50981765/how-to-set-the-orientation-to-portrait-in-firebase-ui-in-android