2016-04-12 1 views
0

Dans Android, il est possible de verrouiller l'orientation de l'écran en ajoutant ceci au manifeste:Comment verrouiller l'orientation de l'écran uniquement pour les téléphones?

android:screenOrientation="landscape" 

Mais est-il possible de verrouiller à l'intérieur de la taille? Je souhaite que mon application soit verrouillée en mode Portrait pour téléphone et que l'utilisateur puisse utiliser à la fois le format portrait et le format paysage sur un format tablette.

J'ai essayé avec la taille de l'écran en utilisant:

Display display = getWindowManager().getDefaultDisplay(); 
Point size = new Point(); 
display.getSize(size); 

mais il ne semble pas fonctionner quand je veux verrouiller l'écran à partir du code. Toute idée d'un bon moyen de le faire?

+0

Vous pouvez changer l'orientation par doin setRequestedOrientation (ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE); avez-vous des problèmes pour obtenir la taille de l'écran? –

+0

First Upall détecter le type de périphérique (tablette ou téléphone), puis définir l'orientation Utiliser: http: //stackoverflow.com/questions/18268218/change-screen-orientation-programatically-using-a-button & http://stackoverflow.com/questions/11330363/how-to-detect-device-is-android-phone-or-android-tablet –

Répondre

2

premier chèque this réponse

maintenant, vous pouvez modifier l'orientation comme celui-ci

boolean tabletSize = getResources().getBoolean(R.bool.isTablet); 
if (!tabletSize) { 
    setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT); 
} 
+1

R.bool.isTablet? –

+1

avez-vous vérifié le lien que je mentionne dans la réponse ?? Veuillez d'abord vérifier ce lien, puis vous comprendrez ce qu'est R.bool.isTablet. –

0

Je ne sais pas exactement son travail ou ne pas essayer cette

TelephonyManager manager = (TelephonyManager)context.getSystemService(Context.TELEPHONY_SERVICE); 
if(manager.getPhoneType() == TelephonyManager.PHONE_TYPE_NONE){ 
    return "Tablet"; 
}else{ 
    return "Mobile"; 
} 

ou regarder ceci fois Determine if the device is a smartphone or tablet?

1

Utiliser cet:

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

public void setOrientation(Context context){ 
if(!isTablet(contex){ 
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT); 
    } 
} 
1

Vous pouvez obtenir la diagonale Valeur du dispositif en suivant

DisplayMetrics metrics = new DisplayMetrics(); 
getActivity().getWindowManager().getDefaultDisplay().getMetrics(metrics); 

float yInches= metrics.heightPixels/metrics.ydpi; 
float xInches= metrics.widthPixels/metrics.xdpi; 
double diagonalInches = Math.sqrt(xInches*xInches + yInches*yInches); 

Après que vous pouvez vérifier en fonction de votre besoin si vous voulez que la taille de 5,5.5,6 , 6.5 ou supérieur et l'orientation des changements en fonction des besoins

if (diagonalInches>=6.5){ 
     // 6.5inch device or bigger 
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT); 
    }else{ 
     // smaller device 
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE); 
    } 
1

Vous pouvez créer une méthode personnalisée pour vérifier la densité actuelle de l'écran du téléphone,

public static boolean isTabLayout(Activity activity) { 

     DisplayMetrics metrics = new DisplayMetrics(); 
     activity.getWindowManager().getDefaultDisplay().getMetrics(metrics); 

     int widthPixels = metrics.widthPixels; 
     int heightPixels = metrics.heightPixels; 

     float scaleFactor = metrics.density; 

     float widthDp = widthPixels/scaleFactor; 
     float heightDp = heightPixels/scaleFactor; 

     float smallestWidth = Math.min(widthDp, heightDp); 

     if (smallestWidth > 720) { 
      //Device is a 10" tablet 
      return true; 
     } else if (smallestWidth > 600) { 
      //Device is a 7" tablet 
      return true; 
     } 
     return false; 
    } 

Pour verrouiller l'orientation de l'écran en mode portrait,

if (!isTabLayout(this)) { 
    setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT); 
    } 

ou dans le paysage,

if (!isTabLayout(this)) { 
    setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE); 
    }