在平板电脑上禁用旋转

我的应用为手机启用了旋转功能,但我不想在平板电脑中使用横向模式。

我尝试过

<activity 
    android:screenOrientation="portrait" />

但这并不只为平板电脑指定纵向模式。

shao1014 回答:在平板电脑上禁用旋转

我认为您将需要以编程方式进行操作,我认为清单没有任何属性可以对其进行设置。

第一步:检测平板电脑中安装的应用,如下所示:

Java:

 public boolean isTablet(Context context) {
        boolean xlarge = ((context.getResources().getConfiguration().screenLayout & Configuration.SCREENLAYOUT_SIZE_MASK) == Configuration.SCREENLAYOUT_SIZE_XLARGE);
        boolean large = ((context.getResources().getConfiguration().screenLayout & Configuration.SCREENLAYOUT_SIZE_MASK) == Configuration.SCREENLAYOUT_SIZE_LARGE);
        return (xlarge || large);
    }

科特林

fun isTablet(context: Context): Boolean {
        val xlarge = context.resources.configuration.screenLayout and Configuration.SCREENLAYOUT_SIZE_MASK === Configuration.SCREENLAYOUT_SIZE_XLARGE
        val large =
            context.resources.configuration.screenLayout and Configuration.SCREENLAYOUT_SIZE_MASK === Configuration.SCREENLAYOUT_SIZE_LARGE
        return xlarge || large
    }

Kotlin扩展名:

    fun Context.isTablet(): Boolean {
        val xlarge = resources.configuration.screenLayout and Configuration.SCREENLAYOUT_SIZE_MASK === Configuration.SCREENLAYOUT_SIZE_XLARGE
        val large =
            resources.configuration.screenLayout and Configuration.SCREENLAYOUT_SIZE_MASK === Configuration.SCREENLAYOUT_SIZE_LARGE
        return xlarge || large
    }

第二步,在onCreate中进行更改:

Java


if(isTablet(this)){
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
}

Kotlin扩展

  override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)

        if(isTablet()){
            setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
        }
    }
,

您可以通过代码进行设置,但也可以仅将其保存在XML文件中,但是诀窍是使用resource qualifiers并为您称为平板电脑使用单独的XML,或者为平板电脑使用不同的样式并应用活动的样式。

本文链接:https://www.f2er.com/3072934.html

大家都在问