2014-09-10 46 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); 
相關問題