~Onlooker 2009-02-24 15:29 采纳率: 0%
浏览 678
已采纳

在 Android 上禁用横屏模式?

How can I disable landscape mode for some of the views in my Android app?

转载于:https://stackoverflow.com/questions/582185/disable-landscape-mode-in-android

  • 写回答

30条回答 默认 最新

  • Lotus@ 2009-02-24 17:04
    关注

    Add android:screenOrientation="portrait" to the activity in the AndroidManifest.xml. For example:

    <activity android:name=".SomeActivity"
        android:label="@string/app_name"
        android:screenOrientation="portrait" />
    

    EDIT: Since this has become a super-popular answer, I feel very guilty as forcing portrait is rarely the right solution to the problems it's frequently applied to.
    The major caveats with forced portrait:

    • This does not absolve you of having to think about activity lifecycle events or properly saving/restoring state. There are plenty of things besides app rotation that can trigger an activity destruction/recreation, including unavoidable things like multitasking. There are no shortcuts; learn to use bundles and retainInstance fragments.
    • Keep in mind that unlike the fairly uniform iPhone experience, there are many devices where portrait is not the clearly popular orientation. When users are on devices with hardware keyboards or game pads a la the Droid 1-4, Xperia Play, or the Nvidia Shield, forcing portrait can make your app a giant usability hassle (especially on the Shield). If your app doesn't have an extremely specific use case that would lead to an outright negative experience for supporting other orientations, you should probably not force landscape. I'm talking about things like, "this is a remote control app for a phone with an IR blaster on only one side of the device," or "this is a cash register app for one specific model of tablet always used in a fixed hardware dock."

    So most apps should just let the phone sensors, software, and physical configuration make their own decision about how the user wants to interact with your app. A few cases you may still want to think about, though, if you're not happy with the default behavior of sensor orientation in your use case:

    • If your main concern is accidental orientation changes mid-activity that you think the device's sensors and software won't cope with well (for example, in a tilt-based game) consider supporting landscape and portrait, but using nosensor for the orientation. This forces landscape on most tablets and portrait on most phones, but I still wouldn't recommend this for most "normal" apps (some users just like to type in the landscape softkeyboard on their phones, and many tablet users read in portrait - and you should let them).
    • If you still need to force portrait for some reason, sensorPortrait may be better than portrait for Android 2.3+; this allows for upside-down portrait, which is quite common in tablet usage.
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(29条)

报告相同问题?

悬赏问题

  • ¥15 交替优化波束形成和ris反射角使保密速率最大化
  • ¥15 树莓派与pix飞控通信
  • ¥15 自动转发微信群信息到另外一个微信群
  • ¥15 outlook无法配置成功
  • ¥30 这是哪个作者做的宝宝起名网站
  • ¥60 版本过低apk如何修改可以兼容新的安卓系统
  • ¥25 由IPR导致的DRIVER_POWER_STATE_FAILURE蓝屏
  • ¥50 有数据,怎么建立模型求影响全要素生产率的因素
  • ¥50 有数据,怎么用matlab求全要素生产率
  • ¥15 TI的insta-spin例程