简体   繁体   English

如何将平板电脑清单中的方向设置为手机的横向和纵向?

[英]How to set up orientation in manifest for tablets as landscape and portrait for phones?

How to set up orientation in manifest for tablets as landscape and portrait for phones? 如何将平板电脑清单中的方向设置为手机的横向和纵向?

I want: 我想要:

    <application 
            android:name=".App"
            android:allowBackup="true"
            android:icon="@drawable/ic_launcher"
            android:installLocation="preferExternal"
            android:label="@string/app_name"
            android:largeHeap="true"

            <!--for phones-->
            android:screenOrientation="portrait"
            <!--for tablets-->
            android:screenOrientation="landscape"
....

In one manifest. 在一个清单中。 Is it possible? 可能吗?

No it's not possible , you have to check that on runtime . 不,这是不可能的,您必须在运行时检查一下。

Check if it's Tablet by : 通过以下方式检查它是否为平板电脑:

public static boolean isTablet(Context context)
{
    return (context.getResources().getConfiguration().screenLayout & Configuration.SCREENLAYOUT_SIZE_MASK)
            >= Configuration.SCREENLAYOUT_SIZE_LARGE;
}

and then in your Activity, set orientation like this before setContentView() 然后在您的活动中,在setContentView()之前设置像这样的方向

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

You can check at run time by calling below method: 您可以在运行时通过调用以下方法进行检查:

boolean isTablet(Context context) {
        boolean isTablet = false;
        DisplayMetrics metrics = context.getApplicationContext().getResources()
                .getDisplayMetrics();
        Display display = ((WindowManager) context.getSystemService("window"))
                .getDefaultDisplay();
        int width = display.getWidth();
        int height = display.getHeight();

        float density = metrics.density;
        if ((width / density >= 600.0F) && (height / density >= 600.0F))
            isTablet = true;
        else {
            isTablet = false;
        }

        return isTablet;
    }

and you can set by orientation by calling in onCreate method 您可以通过调用onCreate方法按方向进行设置

if(isTablet(this)
     setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE); 
 else
     setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT); 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何锁定 Android 应用程序在手机中的纵向和平板电脑中的横向? - How to Lock Android App's Orientation to Portrait in Phones and Landscape in Tablets? 手机为纵向模式,平板电脑为纵向+横向 - Portrait mode for Phones and Portrait+Landscape for Tablets 如何启用手机的纵向和平板电脑的纵向/横向? - How to enable portrait orientation for mobiles and portrait/landscape for tablets? 我如何才能使它只用于平板电脑的风景和手机的肖像? - How do I make it only landscape for tablets and only portrait for phones? 禁用手机横向但允许平板电脑 - Disable landscape orientation for phones but allow for tablets 如何将MediaRecorder方向设置为横向或纵向 - How to set MediaRecorder orientation to landscape or portrait 如何只允许平板电脑同时使用风景/人像 - How to allow both landscape/portrait for tablets only 如何支持不超过4.0的手机(不包括平板电脑)? - How to support phones up to 4.0, excluding tablets? 为手机设置纵向,但为平板电脑设置纵向和横向 - Set portrait for phone but both portrait and landscape orientation for tablet 如何锁定应用程序方向(横向或纵向)? - How is application orientation (landscape or portrait) locked?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM