[英]How to lock screen orientation only for phones?
In android it is possible to lock the screen orientation by adding this to the manifest : 在android中,可以通过将其添加到清单中来锁定屏幕方向:
android:screenOrientation="landscape"
But is it possible to lock within the size? 但是有可能锁定在尺寸范围内吗?
I want my app to be locked in portrait for phone format, and I want the user beeing able to use both portrait and landscape on a tablet format. 我希望我的应用以纵向格式锁定为手机格式,并且希望用户能够在平板电脑格式上同时使用纵向和横向。
I've tried with screen size by using : 我已经尝试通过使用屏幕大小:
Display display = getWindowManager().getDefaultDisplay();
Point size = new Point();
display.getSize(size);
but it doesn't seem to work when I want to lock screen from code. 但是当我想从代码锁定屏幕时,它似乎不起作用。 Any idea of a good way to do this?
您知道执行此操作的好方法吗?
Use This: 用这个:
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);
}
}
You can get the diagonal Value of the device by following 您可以通过以下方法获取设备的对角线值
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);
After that you can check according to your need if you want the size of 5,5.5,6,6.5 or higher and change orientation according to need 之后,您可以根据需要检查是否需要5、5.5、6、6.5或更大的尺寸,并根据需要更改方向
if (diagonalInches>=6.5){
// 6.5inch device or bigger
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
}else{
// smaller device
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
}
You can create a custom method to check the current screen density of 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;
}
To lock the screen orientation in portrait, 要将屏幕方向锁定为纵向,
if (!isTabLayout(this)) {
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
}
or in landscape, 或在风景中
if (!isTabLayout(this)) {
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
}
I don't know exactly its working or not try this 我不知道它的确切作用还是不尝试这个
TelephonyManager manager = (TelephonyManager)context.getSystemService(Context.TELEPHONY_SERVICE);
if(manager.getPhoneType() == TelephonyManager.PHONE_TYPE_NONE){
return "Tablet";
}else{
return "Mobile";
}
or look this once Determine if the device is a smartphone or tablet? 还是只看一次确定设备是智能手机还是平板电脑?
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.