如何在 Android SDK 27 中将屏幕方向限制为纵向

How to restrict screen orientation to portrait in Android SDK 27

我试过了

android:screenOrientation="portrait"

setRequestedOrientation (ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);

但它使应用程序崩溃,是否有其他方法可以在 Android 8.0.0+ 中运行?

Logcat:

FATAL EXCEPTION: main
  Process: in.ajtech.finX, PID: 15077
  java.lang.RuntimeException: Unable to start activity ComponentInfo{in.ajtech.finX/in.ajtech.finX.CalendarActivity}: java.lang.IllegalStateException: Only fullscreen activities can request orientation
      at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2817)
      at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2892)
      at android.app.ActivityThread.-wrap11(Unknown Source:0)
      at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1593)
      at android.os.Handler.dispatchMessage(Handler.java:105)
      at android.os.Looper.loop(Looper.java:164)
      at android.app.ActivityThread.main(ActivityThread.java:6541)
      at java.lang.reflect.Method.invoke(Native Method)
      at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:240)
      at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:767)
   Caused by: java.lang.IllegalStateException: Only fullscreen activities can request orientation
      at android.os.Parcel.readException(Parcel.java:1950)
      at android.os.Parcel.readException(Parcel.java:1888)
      at android.app.IActivityManager$Stub$Proxy.setRequestedOrientation(IActivityManager.java:5675)
      at android.app.Activity.setRequestedOrientation(Activity.java:5739)
      at in.ajtech.finX.CalendarActivity.onCreate(CalendarActivity.java:55)
      at android.app.Activity.performCreate(Activity.java:6975)
      at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1213)
      at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2770)
      at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2892) 
      at android.app.ActivityThread.-wrap11(Unknown Source:0) 
      at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1593) 
      at android.os.Handler.dispatchMessage(Handler.java:105) 
      at android.os.Looper.loop(Looper.java:164) 
      at android.app.ActivityThread.main(ActivityThread.java:6541) 
      at java.lang.reflect.Method.invoke(Native Method) 

您可以从清单文件中设置 属性,在每个 activity 中,添加 android:screenOrientation="portrait"

BUG

阅读Only fullscreen activities can request orientation

Only fullscreen activities can request orientation at android.app.ActivityThread.performLaunchActivity

您应该使用 AppCompatActivity 而不是 Activity.

让你的 activity 延长 AppCompatActivity.

JAVA

public class YourActivity extends AppCompatActivity {
  // ...
}

Kotlin

class  YourActivity : AppCompatActivity()

FYI

Beginning with Android 3.0 (API level 11), all activities that use the default theme have an ActionBar as an app bar. However, app bar features have gradually been added to the native ActionBar over various Android releases. As a result, the native ActionBar behaves differently depending on what version of the Android system a device may be using. By contrast, the most recent features are added to the support library's version of Toolbar, and they are available on any device that can use the support library.

来自 Setting Up the App Bar.

DEMO

设置你的风格

<style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">

</style>

这是 android sdk(27) 问题,您不能将 portraitTranslucent 一起使用,因此将您的目标 sdk 减少到 26 或删除 Translucent 主题或删除 portrait 模式。

extents Activity 重新调整为 AppCompatActivity 解决了这个问题。谢谢大家的帮助

AndroidManifest.xml 中进行以下更改:

  1. 对于不透明的活动,即full-screen,设置:

    android:screenOrientation="portrait"
    android:theme="@android:style/Theme.NoTitleBar"
    

    注意主题不能是半透明的。

  2. 对于透明活动,即 pop-up 对话等,设置:

    android:screenOrientation="unspecified"
    android:theme="@android:style/Theme.Translucent.NoTitleBar"
    

    请注意,此处未指定 screenOrientation。您可以使用半透明主题。

无需降级 SDK 版本即可运行。

当我试图在另一个 activity 中打开 activity 作为对话框时,我遇到了同样的错误。 然后我从清单中的对话框 activity 减速中删除了 android:screenOrientation="portrait",问题就解决了! 这是因为 parent activity 应该负责方向。