2014-09-10 42 views
1

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

我想:

<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" 
.... 

在一个清单。可能吗?

回答

5

不,这是不可能的,你必须在运行时检查。

检查它是否是片中:

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

,然后在你的活动,设置方位像在此之前的setContentView()

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

你可以通过调用下面的方法在运行时检查:

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; 
    } 

你可以设置b y方向通过调用onCreate方法

if(isTablet(this) 
    setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE); 
else 
    setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT); 
相关问题