我需要根据设备类型设置强制方向,以使用相关视图(平板电脑横屏视图和移动设备竖屏视图),设备上无法更改旋转。
因为我想在应用程序启动时为所有活动设置方向,所以我认为我应该在主活动中识别设备类型,该主活动由与视图相关的其他活动扩展。
请问如何以正确的方式做到这一点?
非常感谢任何建议。
我需要根据设备类型设置强制方向,以使用相关视图(平板电脑横屏视图和移动设备竖屏视图),设备上无法更改旋转。
因为我想在应用程序启动时为所有活动设置方向,所以我认为我应该在主活动中识别设备类型,该主活动由与视图相关的其他活动扩展。
请问如何以正确的方式做到这一点?
非常感谢任何建议。
在您的主活动(启动器活动)或应用程序类中使用此功能。
if (isTablet(mContext))
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_SENSOR_LANDSCAPE);
else
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
public static boolean isTablet(Context context) {
return (context.getResources().getConfiguration().screenLayout & Configuration.SCREENLAYOUT_SIZE_MASK)
>= Configuration.SCREENLAYOUT_SIZE_LARGE;
}
<?xml version="1.0" encoding="utf-8"?>
<resources>
<bool name="portrait_only">true</bool>
</resources>
<?xml version="1.0" encoding="utf-8"?>
<resources>
<bool name="portrait_only">false</bool>
</resources>
if(getResources().getBoolean(R.bool.portrait_only)){
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
}
<supports-screens>
元素过滤目标设备,然后定义android:screenOrientation
。因此,您需要为不同的设备创建单独的APK文件。android:screenOrientation="portrait" or
android:screenOrientation="landscape"
如果你不想让它被修复,那么你可以使用下面的代码来获取屏幕尺寸。
Display display = getWindowManager().getDefaultDisplay();
Point size = new Point();
display.getSize(size);
int width = size.x;
int height = size.y;
相应地
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);