【问题标题】:Block screen orientation to Portrait for Phone and allow both orientation for tablets阻止屏幕方向为手机的纵向并允许平板电脑的两个方向
【发布时间】:2019-09-05 09:05:32
【问题描述】:

我尝试在onCreate 中使用以下代码将屏幕orientation 设置为电话中的Portrait,并允许Tablets 中的orientations

if (resources.configuration.screenWidthDp < (resources.getDimension(R.dimen.max_phone_screen_width) /
                    resources.displayMetrics.density)) {
       requestedOrientation = ActivityInfo.SCREEN_ORIENTATION_PORTRAIT
}

此代码的问题是,如果应用程序在手机中以横向模式启动,则应用程序首先以横向模式启动,然后切换到纵向。如何解决这个问题?

【问题讨论】:

    标签: android tabs screen-orientation


    【解决方案1】:

    只需将android:screenOrientation="portrait" 添加到清单中的活动元素即可以纵向模式初始化活动。 然后在onCreate

    if (resources.configuration.screenWidthDp >= (resources.getDimension(R.dimen.max_phone_screen_width) /
                    resources.displayMetrics.density)
        ) {
            requestedOrientation = ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE;
        }
    

    【讨论】:

    • 这并不能解决我的问题。如果我添加了这样,当在平板电脑中以横向模式打开应用程序时,它将首先以纵向模式启动,然后切换到横向
    • 据我了解,您希望它在手机中使用 SCREEN_ORIENTATION_PORTRAIT 在平板电脑中使用 UNSPECIFIC?
    • 是的。对此没有合适的解决方案
    猜你喜欢
    • 1970-01-01
    • 2012-03-26
    • 2016-01-07
    • 2012-03-08
    • 1970-01-01
    • 2016-04-19
    • 1970-01-01
    • 1970-01-01
    • 2018-11-08
    相关资源
    最近更新 更多